Go-mysql-driver: A Go language lightweight, fast MySQL drive

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The go language of the Database/sql package is a MySQL driver.

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
    • Completelysql.RawBytes支持

Environmental requirements

    • Go 1 or more
    • MySQL (Version 4.1 or higher), MariaDB or Percona Se
    • RVer

Installation

Simply use the Go tool to add the installation package to your $gopath in the shell

1 $ go get GitHub. COM/go-SQL-driver/mysql

Use

The use of SQL packages is straightforward:
1. Establish the connection
The first is open,
DB, err: = SQL. Open ("MySQL", "User:password@/dbname")
The DB is a *sql. DB type of pointer, in the subsequent operation, the use of the DB
After open, the actual connection with the database is not established, and the actual connection to the database is done by pinging. In addition, the DB should exist throughout the lifetime of the program, that is, when the program starts, it obtains the DB through open until the program finishes, then close db, rather than often open/close.
Err = db. Ping ()

2. Basic usage
The main methods of DB are:
Query executes the query operation of the database, such as a SELECT statement, returning *rows

Queryrow execution Database Returns a query operation of up to 1 rows, returning *row

PrePare prepares a database query operation and returns a *stmt for subsequent query or execution. This stmt can be executed multiple times, or concurrently

Exec execution number does not return any rows of the database statements, such as the delete operation

The main methods of stmt are:
Exec
Query
Queryrow
Close
Usage is similar to DB
The main methods of rows are:
Cloumns: Return to []string,column names
Scan:
Next:
Close:
See:
http://golang.org/pkg/database/sql/
Https://github.com/go-sql-driver/mysql/wiki/Examples
Https://github.com/VividCortex/go-database-sql-tutorial
This article has many examples, easy to understand

A simple test code:

Java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 Package main Import ( "Database/sql" _ "Github.com/go-sql-driver/mysql" "Log" ) func Insert(db *SQL. DB) { stmt, err := db. Prepare("INSERT into user (username, password) VALUES (?,?)" ) defer stmt. Close() if err! = nil { log. Println(err) return } stmt. Exec("Guotie", "Guotie") stmt. Exec("TestUser", "123123") }func main() { db, err := sql. Open("MySQL", "Root:guotie@/hello") if err! = nil { log. Fatalf("Open Database error:%s\n", err) } defer db. Close() err = db. Ping() if err! = nil { log. Fatal(err) } Insert(db) rows, err := db. Query("SELECT ID, username from user where id =?") , 1) if err! = nil { log. Println(err) } defer rows. Close() var id int var name string For rows. Next() { err := rows. Scan(&ID, &name) if err! = nil { log. Fatal(err) } log. Println(ID, name) } err = rows. ERR() if err! = nil { log. Fatal(err) }}

GitHub address Https://github.com/go-sql-driver/mysql, official address http://godoc.org/github.com/go-sql-driver/mysql.

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.