This is a creation in Article, where the information may have evolved or changed.
First, the characteristics
- Lightweight and fast
- Native go language, no C bindings, only pure Go
- No unsafe operations (type conversions, etc.)
- Dynamic processing of crashed connections
- Dynamic connection Pooling
- Support for queries larger than 16MB
- Completely
sql.RawBytes支持
Second, installation
Simply use the Go tool to add the installation package to your $gopath in the shell
<textarea class="crayon-plain print-no" readonly="" style="color:rgb(0,0,0); font-style:inherit; font-variant:inherit; font-weight:inherit; margin:0px; overflow:hidden; width:617.984px; padding-top:0px; padding-right:5px; padding-left:5px; height:41px; position:absolute; border:none; white-space:pre; word-wrap:normal; z-index:0; font-size:12px!important; line-height:15px!important; font-family:Monaco,MonacoRegular,'Courier New',monospace!important"></textarea>
| 1 |
$GoGetGitHub.com/Go-SQL-Driver/MySQL |
You can also download the source code, and then put it in the project, the download address is as follows
GitHub Address Https://github.com/go-sql-driver/mysql , official address Http://godoc.org/github.com/go-sql-driver/mysql
Third, the use
The use of SQL packages is straightforward:
1. Establish the connection
There are several ways to establish a connection:
< Span style= "White-space:pre" >
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 2nd method is the most commonly used method. For example: DB, err: = SQL. Open ("MySQL", "user:password@tcp (localhost:5555)/dbname?charset=utf8") DB is a *sql. The DB type of pointer, in the subsequent operation, all use Db,err is the error message, if the null (nil) Description of the successful connection, CharSet set the character set. 2. Basic usage Insert Operation DB, err: = db. Prepare (' INSERT user (Name,age,sex) VALUES (?,?,?) ') Checkerr (ERR) res, err: = db. Exec ("Tony", 1) checkerr (ERR) ID, err: = Res. Lastinsertid () Checkerr (err) fmt. PRINTLN (ID) using structured operations here, it is not recommended to use a method that directly stitching SQL statements. The query operation is rows, err: = db. Query ("SELECT * from User") Checkerr (Err) for rows. Next () {var id int var name string var age int var sex int rows. Columns () Err = rows. Scan (&id, &name, &age, &sex) checkerr (err) fmt. Println (ID) fmt. PRINTLN (name) fmt. Println (age) fmt. PRINTLN (Sex)} The query here uses a declaration of 4 independent variable IDs, name, age, and sex to hold the values of each row queried. The operation of the database is typically encapsulated in real-world development, and queries such as this typically take into account the return dictionary type. Constructs Scanargs, values two arrays, each value of Scanargs points to aValues the corresponding value of the address columns, _: = Rows. Columns () Scanargs: = Make ([]interface{}, Len (Columns)) Values: = Do ([]interface{}, Len (Columns)) for I: = range values { Scanargs[i] = &values[i]} for rows. Next () {//saves row data to the record dictionary err = rows. Scan (Scanargs ...) Record: = Make (map[string]string) for I, col: = range values {if col! = nil {Record[columns[i]] = s Tring (Col. ([]byte))}} FMT. Println (Record)} modifies the operation stmt, err: = db. Prepare (' UPDATE user SET age=?,sex=? WHERE id=? ') Checkerr (ERR) res, err: = stmt. Exec (2, 1) checkerr (err) num, err: = Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (num) Delete operation stmt, err: = db. Prepare (' DELETE from user WHERE id=? ') Checkerr (ERR) res, err: = stmt. Exec (1) checkerr (err) num, err: = Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (num) full code modification and deletion operations are simple, similar to inserting data, using only rowsaffected to get the number of rows affected. Package main import ("Database/sql" "Fmt" _ "Github.com/go-sql-driver/mysql") func main () {insert ()}//Insert Dem Ofunc Insert () {db, err: = SQL.Open ("MySQL", "Root:@/test?charset=utf8") Checkerr (Err) stmt, err: = db. Prepare (' INSERT user (Name,age,sex) VALUES (?,?,?) ') Checkerr (ERR) res, err: = stmt. Exec ("Tony", 1) checkerr (ERR) ID, err: = Res. Lastinsertid () Checkerr (err) fmt. Println (ID)}//Queries Demofunc query () {db, err: = SQL. Open ("MySQL", "Root:@/test?charset=utf8") Checkerr (Err) rows, err: = db. Query ("SELECT * from User") Checkerr (ERR)//dictionary type//construct Scanargs, values two arrays, each value of Scanargs points to the address columns of values corresponding to the value, _: = Rows. Columns () Scanargs: = Make ([]interface{}, Len (Columns)) Values: = Do ([]interface{}, Len (Columns)) for I: = Rang E values {scanargs[i] = &values[i]} for rows. Next () {//saves row data to the record dictionary err = rows. Scan (Scanargs ...) Record: = Make (map[string]string) for I, col: = range values {if col! = Nil {Record[col Umns[i]] = string (col. ([]byte))}} FMT. Println (Record)}}//Update data func update () {db, err: = SQL. Open ("MySQL", "Root:@/test?charset=utf8") Checkerr (Err) stmt, err: = db. Prepare (' UPDATE user SET age=?,sex=? WHERE id=? ') Checkerr (ERR) res, err: = stmt. Exec (2, 1) checkerr (err) num, err: = Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (NUM)}//Delete data func remove () {db, err: = SQL. Open ("MySQL", "Root:@/test?charset=utf8") Checkerr (Err) stmt, err: = db. Prepare (' DELETE from user WHERE id=? ') Checkerr (ERR) res, err: = stmt. Exec (1) checkerr (err) num, err: = Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (num)} func checkerr (err error) {if err! = Nil {panic (err)}}