Go操做MySQL數據庫

Go操做MySQL數據庫mysql

主要的結構體DB和基於該結構體的方法git

type DB
func (db DB) Begin() (Tx, error)
func (db *DB) Close() error
func (db *DB) Driver() driver.Driver
func (db *DB) Exec(query string, args ...interface{}) (Result, error)
func (db *DB) Prepare(query string) (*Stmt, error)
func (db *DB) Query(query string, args ...interface{}) (*Rows, error)
func (db *DB) QueryRow(query string, args ...interface{}) *Row

Query && QueryRow

以下代碼示例,github

package main

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

func main() {

	//完整的數據庫鏈接字符串
	//username:password@protocol(address)/dbname?param=value
	db, err := sql.Open("mysql", "root:034039@tcp(127.0.0.1:3306)/account")
	defer db.Close()

	if err != nil {
		log.Println(err)
	}

	rows, err := db.Query("SELECT vin,no FROM vehicle_gps where type= ?", 2)

	if err != nil {
		log.Println(err)
	}

	var vin, no string
	for rows.Next() {
		err := rows.Scan(&vin, &no) //獲取字段,select幾個字段就傳入幾個變量
		if err != nil {
			log.Println(err)
		}
		fmt.Println(vin, no)
	}

	// 查詢單條記錄
	singledRow := db.QueryRow("SELECT id,type FROM vehicle_gps where id = ?", 3901)
	var id, kind int
	singledRow.Scan(&id, &kind)
	fmt.Println(id, kind)
}

Exec

以下代碼示例,sql

package main

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

func main() {

	//完整的數據庫鏈接字符串
	//username:password@protocol(address)/dbname?param=value
	db, err := sql.Open("mysql", "root:034039@tcp(127.0.0.1:3306)/account")

	if err != nil {
		log.Println(err)
	}

	//在這裏進行一些數據庫操做
	defer db.Close()

	//db.Exec 執行查詢
	//rand.Intn(100) 返回100 之內隨機數
	rand.Seed(time.Now().Unix()) //設置隨機數的種子
	random := rand.Intn(100)

	result, err := db.Exec("update vehicle_gps set type = ? where id = ?", random, 8033)
	if err != nil {
		fmt.Println("db.Exec error")
		log.Println(err)
	}
	n, _ := result.RowsAffected()
	fmt.Println(n)
}

Prepare

以下代碼示例數據庫

package main

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

func main() {
	db, err := sql.Open("mysql", "root:034039@tcp(127.0.0.1:3306)/account")
	defer db.Close()

	if err != nil {
		log.Println(err)
	}

	stmt, err := db.Prepare("update vehicle_gps set type = ? where vin = ?")
	defer stmt.Close()

	if err != nil {
		log.Println(err)
	}
	//rand.Intn(10) 返回10 之內隨機數
	rand.Seed(time.Now().Unix()) //設置隨機數的種子
	random := rand.Intn(100)

	//Exec方法返回的是sql.Result對象
	result, err := stmt.Exec(random, "JM7CW09FXC0106372")

	if err != nil {
		log.Println(err)
	}

	rows, err := db.Query("select type from vehicle_gps where vin = ?", "JM7CW09FXC0106372")

	if err != nil {
		log.Println(err)
	}

	var t int
	for rows.Next() {
		rows.Scan(&t)
		fmt.Println(t)
		if t != random {
			panic("update type error")
		}
	}

	n, _ := result.RowsAffected()
	fmt.Println(n)
}

事務的支持

以下代碼示例dom

package main

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

func main() {
	db, err := sql.Open("mysql", "root:034039@tcp(127.0.0.1:3306)/account")
	defer db.Close()

	if err != nil {
		log.Println(err)
	}

	tx, err := db.Begin() //開啓一個事物
	defer tx.Commit()     //必定要commit

	if err != nil {
		log.Println(err)
	}

	stmt, err := tx.Prepare("update vehicle_gps set type = ? where vin = ?")
	defer stmt.Close()

	if err != nil {
		log.Println(err)
	}
	//rand.Intn(10) 返回10 之內隨機數
	rand.Seed(time.Now().Unix()) //設置隨機數的種子
	random := rand.Intn(100)
	result, err := stmt.Exec(random, "JM7CW09FXC0106372")

	if err != nil {
		log.Println(err)
	}
	fmt.Println(random)

	if n, _ := result.RowsAffected(); n == 2 {
		fmt.Println("rollback")
		tx.Rollback() //回滾事務後同時也要提交
	}
}

=========END=========tcp

相關文章
相關標籤/搜索