溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

GO語言中怎么實(shí)現(xiàn)Mysql數(shù)據(jù)庫的CURD操作

發(fā)布時間:2021-08-05 14:05:31 來源:億速云 閱讀:181 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)GO語言中怎么實(shí)現(xiàn)Mysql數(shù)據(jù)庫的CURD操作,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

一、先導(dǎo)入驅(qū)動包和增強(qiáng)版Mysql操作庫Sqlx
package main

import (
    "fmt"
    //并不需要使用其API,只需要執(zhí)行該包的init方法(加載MySQL是驅(qū)動程序)
    _ "github.com/go-sql-driver/mysql"
    "github.com/jmoiron/sqlx"
)

此處需要導(dǎo)入導(dǎo)入mysql驅(qū)動包和增強(qiáng)版Mysql操作庫Sqlx。
如果不清楚如何導(dǎo)入第三方包,請查看我的技術(shù)博客:手把手教你怎么使用Go語言第三方庫。

二、insert操作

//執(zhí)行insert操作
func main()  {
    //連接數(shù)據(jù)庫
    //driverName:mysql,表示驅(qū)動器的名稱是mysql也就上面"github.com/go-sql-driver/mysql"導(dǎo)入的驅(qū)動器。
    //dataSourceName:root:123456@tcp(localhost:3306)/mydb 賬戶名:密碼@tcp(ip:端口)/數(shù)據(jù)庫名稱
    //sqlx.Open返回一個*sqlx.DB和錯誤。
    db, _ := sqlx.Open("mysql", "root:123456@tcp(localhost:3306)/mydb")
    defer db.Close()
    //執(zhí)行增刪改
    //query里面是sql語句。
    result, e := db.Exec("insert into person(name,age,rmb,gender,brithday) values(?,?,?,?,?);", "小揚(yáng)", 21, 8888, true, 20000101)
    if e!=nil{
        fmt.Println("err=",e)
        return
    }
    // RowsAffected returns the number of rows affected by an
    // update, insert, or delete. Not every database or database
    // driver may support this.
    rowsAffected, _ := result.RowsAffected()
    // LastInsertId returns the integer generated by the database
    // in response to a command. Typically this will be from an
    // "auto increment" column when inserting a new row. Not all
    // databases support this feature, and the syntax of such
    // statements varies.
    lastInsertId, _ := result.LastInsertId()
    fmt.Println("受影響的行數(shù)=",rowsAffected)
    fmt.Println("最后一行的ID=",lastInsertId)
}

使用sqlx包的Open連接數(shù)據(jù)庫。

driverName:mysql,表示驅(qū)動器的名稱是mysql也就上面"github.com/go-sql-driver/mysql"導(dǎo)入的驅(qū)動器。
dataSourceName是root:123456@tcp(localhost:3306)/mydb 它的含義是 賬戶名:密碼@tcp(ip:端口)/數(shù)據(jù)庫名稱。
sqlx.Open返回一個*sqlx.DB和錯誤。
然后執(zhí)行db.Exec()操作。

result, e := db.Exec("insert into person(name,age,rmb,gender,brithday) values(?,?,?,?,?);", "小揚(yáng)", 21, 8888, true, 20000101)

第一個參數(shù)是query語句。

rowsAffected, _ := result.RowsAffected()
lastInsertId, _ := result.LastInsertId()

RowsAffected()求受影響的行數(shù)。RowsAffected返回update, insert, or delete影響的行數(shù)。不是每一個數(shù)據(jù)庫和數(shù)據(jù)庫驅(qū)動可能支持這個。
LastInsertId()求插入的最后一行的ID。
LastInsertId返回?cái)?shù)據(jù)庫生成的最后一個ID。通常,這來自插入新行時的“自動遞增”列。不是所有數(shù)據(jù)庫都支持此功能。

GO語言中怎么實(shí)現(xiàn)Mysql數(shù)據(jù)庫的CURD操作

三、delete操作

result, e := db.Exec("delete from person where name not like ?;", "%揚(yáng)")

還是執(zhí)行db.Exec(),第一個參數(shù)是delete語句。

查看該操作是否執(zhí)行成功。

GO語言中怎么實(shí)現(xiàn)Mysql數(shù)據(jù)庫的CURD操作

成功?。?!試一試吧!

四、update操作

result, e := db.Exec("update person set name = ? where id = ?;", "大揚(yáng)", 1)

GO語言中怎么實(shí)現(xiàn)Mysql數(shù)據(jù)庫的CURD操作

成功執(zhí)行!

來看一看結(jié)果吧!

GO語言中怎么實(shí)現(xiàn)Mysql數(shù)據(jù)庫的CURD操作

現(xiàn)在可以看到數(shù)據(jù)更新成功。將id為1的數(shù)據(jù)的name項(xiàng)更新為”大揚(yáng)“。
這里兩個?,后面就要有兩個參數(shù)。

五、select操作

package main

import (
    "fmt"
    //并不需要使用其API,只需要執(zhí)行該包的init方法(加載MySQL是驅(qū)動程序)
    _ "github.com/go-sql-driver/mysql"
    "github.com/jmoiron/sqlx"
)

type Person struct {
    // 對應(yīng)name表字段
    Name string `db:"name"`
    // 對應(yīng)age表字段
    Age int `db:"age"`
    // 對應(yīng)rmb表字段
    Money float64 `db:"rmb"`
}

func main()  {
    db, _ := sqlx.Open("mysql", "root:123456@tcp(localhost:3306)/mydb")
    defer db.Close()

    //預(yù)定義Person切片用于接收查詢結(jié)果
    var ps []Person
    //執(zhí)行查詢,得到Perosn對象的集合,丟入預(yù)定義的ps地址
    e := db.Select(&ps, "select name,age,rmb from person where name like ?;", "%揚(yáng)")
    if e != nil{
        fmt.Println("err=",e)
    }
    fmt.Println("查詢成功",ps)
}

Person結(jié)構(gòu)體里面的屬性對應(yīng)數(shù)據(jù)庫里面的字段。比如:

Age int `db:"age"`

表示Age對應(yīng)表里面的字段age。

type Person struct {
 // 對應(yīng)name表字段
 Name string `db:"name"`
 // 對應(yīng)age表字段
 Age int `db:"age"`
 // 對應(yīng)rmb表字段
 Money float64 `db:"rmb"`
}
var ps []Person

因?yàn)椴樵兊慕Y(jié)果可能為多條,所以使用Person切片。然后將查詢結(jié)果放入ps中
提示:要使用ps的指針!

e := db.Select(&ps, "select name,age,rmb from person where name like ?;", "%揚(yáng)")

下面我們來看看查詢結(jié)果:

GO語言中怎么實(shí)現(xiàn)Mysql數(shù)據(jù)庫的CURD操作

上述就是小編為大家分享的GO語言中怎么實(shí)現(xiàn)Mysql數(shù)據(jù)庫的CURD操作了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI