This is a creation in Article, where the information may have evolved or changed.
Use MySQL database in Go language
1. mysql Driver
Go in support of MySQL driver is more, there are several, some are support database/sql standard, and some are using their own implementation interface, commonly used are the following:
- Https://github.com/Go-SQL-Driver/MySQL support Database/sql, all with Go write.
- Https://github.com/ziutek/mymysql supports Database/sql, and also supports custom interfaces, all with go writing.
- Https://github.com/Philio/GoMySQL does not support Database/sql, custom interfaces, all with go write.
2. Sample Code
In the next few sections we will use the same database table structure: Database test, User table UserInfo, associated user Information table Userdetail.
CREATE TABLE' userinfo ' (' uid ' )INT(Ten) not NULLauto_increment, ' username 'VARCHAR( -)NULL DEFAULT NULL, ' Departname 'VARCHAR( -)NULL DEFAULT NULL, ' created ' DATENULL DEFAULT NULL, PRIMARY KEY(' uid '))CREATE TABLE' userdetail ' (' uid ' )INT(Ten) not NULL DEFAULT '0', ' intro 'TEXT NULL, ' profile 'TEXT NULL, PRIMARY KEY(' uid '))
The following example demonstrates how to use the Database/sql interface to perform a pruning operation on a database table
Package Mainimport (_"Github.com/go-sql-driver/mysql" "Database/sql" "FMT"//"Time") Func main () {db, err:= SQL. Open ("MySQL", "Astaxie:astaxie@/test?charset=utf8") Checkerr (err)//Inserting Datastmt, err: = db. Prepare ("INSERT userinfo SET username=?,departname=?,created=?")) Checkerr (ERR) res, err:= stmt. Exec ("Astaxie", "Research and Development department", "2012-12-09") Checkerr (err) ID, err:=Res. Lastinsertid () Checkerr (err) fmt. Println (ID)//Update Datastmt, err = db. Prepare ("Update userinfo set username=?") where uid=? ") Checkerr (ERR) res, err= stmt. Exec ("Astaxieupdate", id) checkerr (ERR) affect, err:=Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (Affect)//Querying DataRows, err: = db. Query ("SELECT * from UserInfo") Checkerr (err) forrows. Next () {varUidint varUsername StringvarDepartment Stringvarcreated string err= Rows. Scan (&uid, &username, &department, &created) Checkerr (err) fmt. PRINTLN (UID) fmt. PRINTLN (username) fmt. PRINTLN (department) FMT. Println (created)}//Delete Datastmt, err = db. Prepare ("Delete from userinfo where uid=?")) Checkerr (ERR) res, err=stmt. Exec (ID) checkerr (ERR) affect, err=Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (affect) db. Close ()}func Checkerr (err error) {ifErr! =Nil {panic (err)}}
We can see from the above code that the go Operation MySQL database is very convenient.
A few key functions I'll explain:
Sql. The open () function is used to turn on a registered database driver, the MySQL database driver is registered in Go-mysql-driver, and the second parameter is the DNS (Data Source Name). It is a database link and configuration information defined by Go-mysql-driver. It supports the following formats:
user@unix(/path/to/socket)/dbname?charset=utf8user:password@tcp(localhost:5555)/dbname?charset=utf8user:password@/dbnameuser:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname
Db. The Prepare () function is used to return the SQL operation that is ready to be executed and then return to the ready execution state.
Db. The Query () function is used to directly execute SQL to return the rows result.
stmt. The Exec () function is used to execute stmt prepared SQL statements
We can see that the parameters we pass in are all =? corresponding data, so that the way to do this can prevent SQL injection to some extent.