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.go Package
main
import (
"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)
//Encapsulated SQL instruction
stmt, err: = db. Prepare ("update ch_playerequip set level = 1")
Iserr (ERR)
//Execution 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 int for
rows. Next () {
rows. Scan (&playerid, &configid, &id, &leave, &isdress)
FMT. Print (playerID, configid, id, leave, isdress)
}
}