MySQL Go驱动

MySQL基础见菜鸟教程;
本文参考了astaxie/build-web-application-with-golang.

创建数据库

编写test.sql


source test.sql 文件


编写Go文件

test.go 内容如下:

package main

import (
    "database/sql"
    "fmt"

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

func main() {
    db, err := sql.Open("mysql", "root:root@/test") //打开数据库test
    checkErr(err)
    stm, err := db.Prepare("DROP TABLE IF EXISTS userinfo;") //准备SQL语句,删除数据表
    checkErr(err)
    _, err = stm.Exec() //Excute, 执行语句
    checkErr(err)
    stm, err = db.Prepare(`CREATE TABLE userinfo (
                                uid INT(10) NOT NULL AUTO_INCREMENT,
                                name VARCHAR(64) NOT NULL DEFAULT '匿名',
                                city VARCHAR(64) NULL DEFAULT '不详',
                                moment DATE NOT NULL DEFAULT '1949-10-10',
                                PRIMARY KEY (uid)
                            ) DEFAULT CHARSET=utf8;`) //创建数据表,设置utf8以支持中文字符
    checkErr(err)
    _, err = stm.Exec()
    checkErr(err)

    //增加数据
    stm, err = db.Prepare("INSERT userinfo SET name=?, city=?, moment=?") //准备SQL语句
    checkErr(err)
    _, err = stm.Exec("诸葛亮", "山东临沂", "234-10-8") //Excute, 传入参数并执行
    checkErr(err)
    _, err = stm.Exec("关羽", "山西运城", "220-1-1")
    checkErr(err)
    _, err = stm.Exec("荀彧", "河南许昌", "212-1-1")
    checkErr(err)

    stm, err = db.Prepare("INSERT userinfo SET city=?")
    checkErr(err)
    res, err := stm.Exec("河南禹州")
    id, err := res.LastInsertId()
    checkErr(err)
    fmt.Println("最后插入的用户序号为:", id)

    //查询数据
    rows, err := db.Query("SELECT * FROM userinfo")
    checkErr(err)
    fmt.Println("打印数据表的每行信息:")
    fmt.Println("---------------------")

    for rows.Next() {
        var uid int
        var name string
        var city string
        var moment string
        err = rows.Scan(&uid, &name, &city, &moment)
        checkErr(err)

        fmt.Print(uid, " ")
        fmt.Print(name, " ")
        fmt.Print(city, " ")
        fmt.Println(moment)
    }

    //删除数据
    stm, err = db.Prepare("DELETE FROM userinfo WHERE uid=?")
    checkErr(err)
    res, err = stm.Exec(2)
    checkErr(err)
    fmt.Println("删除了第2行")

    //更改数据
    stm, err = db.Prepare("UPDATE userinfo SET name=? WHERE uid=? OR uid=?")
    checkErr(err)
    res, err = stm.Exec("郭嘉", id-1, id)
    checkErr(err)
    affect, err := res.RowsAffected()
    checkErr(err)
    fmt.Println("总共有", affect, "行的信息发生了更改")

    //查询数据
    rows, err = db.Query("SELECT * FROM userinfo")
    checkErr(err)
    fmt.Println("打印数据表的每行信息:")
    fmt.Println("---------------------")

    for rows.Next() {
        var uid int
        var name string
        var city string
        var moment string
        err = rows.Scan(&uid, &name, &city, &moment)
        checkErr(err)

        fmt.Print(uid, " ")
        fmt.Print(name, " ")
        fmt.Print(city, " ")
        fmt.Println(moment)
    }

    db.Close()
}

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

运行结果:


进入MySQL查看数据表:


你可能感兴趣的:(MySQL Go驱动)