用Go-SQL-Driver访问mysql数据库

go语言提供了数据库访问的统一接口,但是自己自己的具体数据库访问驱动实现。
开源比较常用的有go-sql-driver/mysql(原来的go-mysql-driver)和mymysql
我个人比较倾向于go-sql-driver,实现规范,通用性强
Go-SQL-Driver是它的新名字,已经迁移到了gitbub,地址:https://github.com/Go-SQL-Driver/MySQL
安装使用很简单
$ go get github.com/Go-SQL-Driver/MySQL
就按装到GOPATH下了
使用也简单,拿他的例子:
新建一张表

CREATE TABLE `squareNum` (
  `number` int(11) NOT NULL,
  `squareNumber` int(11) NOT NULL,
  PRIMARY KEY (`number`)
)

 数据库操作

package main

import (
	"database/sql"
	"fmt"
	_ "github.com/Go-SQL-Driver/MySQL"  //引入mysql驱动
)

func main() {
	/*DSN数据源名称
		[username[:password]@][protocol[(address)]]/dbname[?param1=value1¶mN=valueN]
		user@unix(/path/to/socket)/dbname 
		user:password@tcp(localhost:5555)/dbname?charset=utf8&autocommit=true 
		user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname?charset=utf8mb4,utf8 
		user:password@/dbname 
		无数据库: user:password@/
	*/
	db, err := sql.Open("mysql", "root:123456@tcp(192.168.1.203:3306)/test") //第一个参数数驱动名
	if err != nil {
		panic(err.Error())
	}
	defer db.Close()

	// 欲编译语句,插入数据,这个是标准的go接口,所以只要标准sql,其他数据库通用的,只要换上面的驱动名
	stmtIns, err := db.Prepare("INSERT INTO squareNum VALUES( ?, ? )" ) // ? = 占位符
	if err != nil {
		panic(err.Error())
	}
	defer stmtIns.Close() // main结束是关闭

	// Prepare statement for reading data
	stmtOut, err := db.Prepare("SELECT squareNumber FROM squareNum WHERE number = ?")
	if err != nil {
		panic(err.Error())
	}
	defer stmtOut.Close()

	// Insert square numbers for 0-24 in the database
	for i := 0; i < 25; i++ {
		_, err = stmtIns.Exec(i, (i * i)) // 执行插入
		if err != nil {
			panic(err.Error())
		}
	}

	var squareNum int // we "scan" the result in here

	// Query the square-number of 13
	err = stmtOut.QueryRow(13).Scan(&squareNum) // WHERE number = 13
	if err != nil {
		panic(err.Error())
	}
	fmt.Printf("The square number of 13 is: %d", squareNum)

	// Query another number.. 1 maybe?
	err = stmtOut.QueryRow(1).Scan(&squareNum) // WHERE number = 1
	if err != nil {
		panic(err.Error())
	}
	fmt.Printf("The square number of 1 is: %d", squareNum)
}

 本文出处:http://blog.gcove.net/go-sql-driver-mysql.html

你可能感兴趣的:(go)