Golang:开源库 Go-MySQL-Driver 使用

Golang:开源库 Go-MySQL-Driver 使用

A MySQL-Driver for Go's database/sql package

MySQL

MySQL is the world's most popular open source database. Whether you are a fast growing web property, technology ISV or large enterprise, MySQL can cost-effectively help you deliver high performance, scalable database applications. —— MySQL

MySQL驱动 Go-MySQL-Driver 使用

github 地址

Go-MySQL-Driver

文档

Go-MySQL-Driver wiki

安装

$ go get -u github.com/go-sql-driver/mysql

导入

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

数据库表结构

USE test;
CREATE TABLE `squareNum` (
    `number` INT(11) NOT NULL DEFAULT '0',
    `squareNumber` INT(11) NOT NULL DEFAULT '0',
    PRIMARY KEY (`number`)
);
/*
+--------------+---------+------+-----+---------+-------+
| Field        | Type    | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| number       | int(11) | NO   | PRI | NULL    |       |
| squareNumber | int(11) | NO   |     | NULL    |       |
+--------------+---------+------+-----+---------+-------+
*/

用例

package main

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

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

    // Open doesn't open a connection. Validate DSN data:
    err = db.Ping()
    checkErr(err)

    // Use the DB normally, execute the querys etc
    stmtIns, err := db.Prepare("INSERT INTO squareNum VALUES( ?, ? )")
    checkErr(err)
    defer stmtIns.Close() // Close the statement when we leave main() / the program terminates

    stmtOut, err := db.Prepare("SELECT squareNumber FROM squareNum WHERE number = ?")
    checkErr(err)
    defer stmtOut.Close()
    for i := 0; i < 25; i++ {
                _, err = stmtIns.Exec(i, (i * i)) // Insert tuples (i, i^2)
                if err != nil {
                        panic(err.Error()) // proper error handling instead of panic in your app
                }
        }

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

    // Query the square-number of 13
        err = stmtOut.QueryRow(13).Scan(&squareNum) // WHERE number = 13
    checkErr(err)
    fmt.Printf("The square number of 13 is: %d\n", squareNum)

    // Query another number.. 1 maybe?
        err = stmtOut.QueryRow(1).Scan(&squareNum) // WHERE number = 1
        checkErr(err)
        fmt.Printf("The square number of 1 is: %d\n", squareNum)

    stmtIns, err = db.Prepare("DELETE FROM squareNum where number=?")
        checkErr(err)

    res, err := stmtIns.Exec(13)
        checkErr(err)

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

    fmt.Println("affect:", affect)
}

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

/*
Output:
The square number of 13 is: 169
The square number of 1 is: 1
affect: 1
*/

以上

本文链接:/2018/06/21/golang-go-mysql-driver/
请尊重作者的劳动成果,转载请注明出处!Sakishum 博客保留对文章的所有权利。

Pingbacks已打开。

引用地址

评论已关闭