go-Database Connection

Source: Internet
Author: User

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)
	}
}



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.