Go Operation MySQL Driver Pack a lot, here to explain the more popular Go-sql-driver/mysql
1. Download and install
Perform the following two commands:
Download: Go get github.com/go-sql-driver/mysql
Installation: Go install Github.com/go-sql-driver/mysql
Files after the installation is complete
Root/go_s is the Go working directory, environment variable: The value set in the $GOPATH
2. Importing Packages
Import (
"Database/sql"
_ "Github.com/go-sql-driver/mysql"
)
3. Link Database
Open function:
DB, err: = SQL. Open ("MySQL", "Username: Password @tcp (IP: Port)/database? Charset=utf8")
For example: DB, err: = SQL. Open ("MySQL", "Root:[email Protected] (127.0.0.1:3306)/test?charset=utf8")
4. Adding and deleting changes
The table structure in the following example:
CREATE TABLE ' UserInfo ' (
' UID ' int (ten) not NULL auto_increment,
' username ' varchar (+) DEFAULT NULL,
' Departname ' varchar (+) DEFAULT NULL,
' Created ' Date DEFAULT NULL,
PRIMARY KEY (' uid ')
) Engine=innodb auto_increment=7 DEFAULT Charset=utf8;
:
Increase
There are two ways of doing this:
1. Use the EXEC function directly to add
result, err: = db. Exec ("INSERT into UserInfo (username, departname, created) VALUES (?,?,?)", "Lily", "Sales", "2016-06-21")
2. First use prepare to get stmt, then call exec to add
stmt, err: = db. Prepare ("INSERT userinfo SET username=?,departname=?,created=?")
Res, err: = stmt. Exec ("Zhja", "research and Development", "2016-06-17")
Another frequently used feature to get the self-increment ID of the data just added
ID, Err: = Res. Lastinsertid ()
Related code:
Delete
Delete the same as the increment syntax above, just replace the INSERT statement with the DELETE statement
Modify
The modification is the same as the increment syntax above, just to change the INSERT statement to the UPDATE statement
Inquire
Querying a single data, Queryeow function
var username, Departname, created string
ERR: = db. Queryrow ("Select username,departname,created from UserInfo WHERE uid=?", 3). Scan (&username, &departname, &created)
Code:
Querying multiple data, and traversing
Query gets the data for XXX. Next () traverse the data
5. Business
Execute before operating the database, DB. Begin ()
Example: TX, err: = db. Begin ()
Save to Database: err: = Tx.commit ()
Rollback: Err: = TX. Rollback ()
Note that after you set up a transaction, the operation database is not db, but TX
The following is the source code of the test file when writing the article,//the content of the comment please delete the test yourself
Package Mainimport (//"FMT" "Database/sql" _ "Github.com/go-sql-driver/mysql") type userinfo struct { Usernamestringdepartnamestringcreatedstring}func Main () {db, err: = SQL. Open ("MySQL", "root:[email protected" (127.0.0.1:3306)/test?charset=utf8 ") Checkerr (Err)//insert//stmt, err: = Db. Prepare ("INSERT userinfo SET username=?,departname=?,created=?") Checkerr (Err)//res, err: = stmt. Exec ("Zhja", "research and Development", "2016-06-17")//checkerr (Err)//id, err: = Res. Lastinsertid ()//checkerr (err)//fmt. Println (ID)//result, err: = db. Exec ("INSERT into UserInfo (username, departname, created) VALUES (?,?,?)", "Lily", "Sales", "2016-06-21")//checkerr (ERR)// IDS, err: = result. Lastinsertid ()//fmt. Println (IDs)//db. Exec ("DELETE from UserInfo WHERE uid=?", 1)//checkerr (err)//stmt, err: = db. Prepare ("DELETE from UserInfo WHERE uid=?") stmt. Exec (2)//var username, departname, created string//err = db. Queryrow ("Select username,departname,created from UserInfo WHERE uid=?", 3). Scan (&username, &departname, &created)Fmt. PRINTLN (username)//fmt. Println (Departname)//fmt. PRINTLN (created) rows, err: = db. Query ("Select username,departname,created from UserInfo WHERE username=?", "Zhja") Checkerr (Err) for rows. Next () {var username, Departname, created stringif err: = rows. Scan (&username, &departname, &created); Err = = Nil {fmt. PRINTLN (Err)}fmt. PRINTLN (username) fmt. Println (Departname) fmt. Println (created)}tx, err: = db. Begin () Checkerr (err) stmt, err1: = Tx. Prepare ("INSERT into UserInfo (username, departname, created) VALUES (?,?,?)") Checkerr (ERR1) _, Err2: = stmt. Exec ("Test", "testing", "2016-06-20") Checkerr (ERR2)//err3: = Tx.commit () ERR3: = Tx. Rollback () Checkerr (ERR3)}func Checkerr (err error) {if err! = Nil {panic (err)}}
Go Operation Database Go-sql-driver/mysql use detailed