go-Database Connection

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The GO src comes with the SQL package and the package that connects to the database. Here is an example of connecting MySQL.

Sql. Open opens up a database connection. When you execute EXEC or query, you make the connection yourself. So the entire application only needs to initialize the sql.db pointer once. Then use it everywhere and it's OK.

DbTest Project Main.gopackage mainimport ("Database/sql" "Fmt" _ "Github.com/go-sql-driver/mysql" "Time") Func Iserr ( Err error) {if err! = Nil {fmt. Printf ("Cheng Error:%s", err)}}func main () {//Initialize database information, this is a database connection pool db, err: = SQL. Open ("MySQL", "test:123456@tcp (192.168.33.9:3306)/jxy2_s001") Iserr (ERR)//Package SQL instruction stmt, err: = db. Prepare ("update ch_playerequip set level =  1") Iserr (ERR)//execute impact instruction result, err: = stmt. Exec () Iserr (err) fmt. Print (result. Rowsaffected ())//Execute query rows, err: = db. Query ("Select Playerid,id,configid,isdress,level from Ch_playerequip") Iserr (ERR) defer rows. Close () var playerID, Configid, id, leave, isdress intfor rows. Next () {rows. Scan (&playerid, &configid, &id, &leave, &isdress) fmt. Print (playerID, configid, id, leave, isdress)}}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.