Go的go-sql-driver/mysql

函数

Open()    –  creates a DB
Close()   - closes the DB
Query()   - 查询
QueryRow()-查询行
          -通过 rows.Next() 迭代查询数据.
          -通过 rows.Scan() 读取每一行的值
          - for rows.Next(){
        		var id,name,content string 
		        if err:=rows.Scan(&id,&name,&content);err!=nil {
			        return res,err
       		}
		    c:=&defs.Comment{Id:id,VideoId:vid,Author:name,Content:content}
		    res=append(res,c)
	        }
Exec()    -执行操作,update,insert,delete
Row       - A row is not a hash map, but an abstraction of a cursor
Next()    -遍历
Scan()    -字段值:根据名字查询出密码值
          -err=stmtOut.QueryRow(loginName).Scan(&pwd)

基础

下载:go get github.com/Go-SQL-Driver/MySQL
安装:go install github.com/Go-SQL-Driver/MySQL

导入:

  import (
          "database/sql"
          _"github.com/go-sql-driver/mysql"
  )

链接

Open函数:
db, err := sql.Open("mysql", "用户名:密码@tcp(IP:端口)/数据库?charset=utf8")
例如:db, err := sql.Open("mysql", "root:111111@tcp(127.0.0.1:3306)/test?charset=utf8")

 

  CREATE TABLE `userinfo` (
      `uid` int(10) NOT NULL AUTO_INCREMENT,
      `username` varchar(64) DEFAULT NULL,
      `departname` varchar(64) DEFAULT NULL,
      `created` date DEFAULT NULL,
      PRIMARY KEY (`uid`)
   ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

CRUD

package main

import (
    "database/sql"
    "fmt"

    _ "github.com/go-sql-driver/mysql"
)

func main() {
    db, err := sql.Open("mysql", "root:wangshubo@/test?charset=utf8")
    checkErr(err)

    // insert
    stmt, err := db.Prepare("INSERT user_info SET id=?,name=?")
    checkErr(err)

    res, err := stmt.Exec(1, "wangshubo")
    checkErr(err)

    // update
    stmt, err = db.Prepare("update user_info set name=? where id=?")
    checkErr(err)

    res, err = stmt.Exec("wangshubo_update", 1)
    checkErr(err)

    affect, err := res.RowsAffected()
    checkErr(err)

    fmt.Println(affect)

    // query
    rows, err := db.Query("SELECT * FROM user_info")
    checkErr(err)

    for rows.Next() {
        var uid int
        var username string

        err = rows.Scan(&uid, &username)
        checkErr(err)
        fmt.Println(uid)
        fmt.Println(username)
    }

    // delete
    stmt, err = db.Prepare("delete from user_info where id=?")
    checkErr(err)

    res, err = stmt.Exec(1)
    checkErr(err)

    // query
    rows, err = db.Query("SELECT * FROM user_info")
    checkErr(err)

    for rows.Next() {
        var uid int
        var username string

        err = rows.Scan(&uid, &username)
        checkErr(err)
        fmt.Println(uid)
        fmt.Println(username)
    }

    db.Close()

}

func checkErr(err error) {    
    if err != nil {
        panic(err)
    }
}

查询

查询单条数据,QueryEow 函数

var username, departname, created string
err := db.QueryRow("SELECT username,departname,created FROM userinfo WHERE uid=?", 3).Scan(&username, &departname, &created)

查询多条数据,并遍历Query 获取数据,for xxx.Next() 遍历数据

    rows, err := db.Query("SELECT username,departname,created FROM userinfo WHERE username=?", "zhja")
    checkErr(err)
    for rows.Next() {
        var username, departname, created string
        if 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", "测试", "2016-06-20")
    checkErr(err2)
    //err3 := tx.Commit()
    err3 := tx.Rollback()
    checkErr(err3)

 

你可能感兴趣的:(Go)