1. 程式人生 > >Golang之Mysql操作

Golang之Mysql操作

set ron display imp lan fun 圖片 try 增刪改

話說當年武大郎對著電腦一頓劈裏啪啦,,,對mysql增刪改查

增加insert

技術分享圖片
package main

import (
    "fmt"
    "github.com/jmoiron/sqlx"
    _ "github.com/go-sql-driver/mysql" //初始化一個mysql驅動,必須
)

type Person struct {
    UserId   int    `db:"user_id"`
    Username string `db:"username"`
    Sex      string `db:"sex"`
    Email    
string `db:"email"` } type Place struct { Country string `db:"country"` City string `db:"city"` TelCode int `db:"telcode"` } var Db *sqlx.DB func init() { //"mysql"指定數據庫類型, /test指定數據庫 root:123 冒號隔開密碼 root賬號 123密碼 database, err := sqlx.Open("mysql", "root@tcp(127.0.0.1:3306)/test
") if err != nil { fmt.Println("open mysql failed,", err) return } Db = database } func main() { //插入數據入表 r, err := Db.Exec("insert into person(username,sex,email)values(?,?,?)", "chaoge", "man", "[email protected]") if err != nil { fmt.Println("
exec failed,", err) return } id, err := r.LastInsertId() if err != nil { fmt.Println("exec failed,", err) return } fmt.Println("insert succ:", id) }
insert

查詢select

技術分享圖片
package main

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

type Person struct {
    UserId   int    `db:"user_id"‘`
    Username string `db:"username"`
    Sex      string `db:"sex"`
    Email    string `db:"email"`
}
type Place struct {
    Country string `db:"country"`
    City    string `db:"city"`
    TelCode int    `db:"telcode"`
}

var Db *sqlx.DB

func init() {
    //初始化鏈接數據庫
    database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")
    if err != nil {
        fmt.Println("open mysql failed", err)
        return
    }
    Db = database
}

func main() {
    var person []Person
    err := Db.Select(&person, "select user_id,username,sex,email from person where user_id=?", 1)
    if err != nil {
        fmt.Println("exec failed,", err)
        return
    }
    fmt.Println("select succ:", person)

}
select

改update

技術分享圖片
package main

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

type Person struct {
    UserId   int    `db:"user_id"`
    Username string `db:"username"`
    Sex      string `db:"sex"`
    Email    string `db:"email"`
}
type Place struct {
    Country string `db:"country"`
    City    string `db:"city"`
    TelCode int    `db:"telcode"`
}

var Db *sqlx.DB

func init() {
    database, err := sqlx.Open("mysql", "root@tcp(127.0.0.1:3306)/test")
    if err != nil {
        fmt.Println("open mysql failed,", err)
        return
    }
    Db = database
}
func main() {
    _, err := Db.Exec("update person set username=? where user_id=?", "chaoge666", 1)
    if err != nil {
        fmt.Println("exec failed,", err)
    }
}
update

刪delete

技術分享圖片
package main

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

type Person struct {
    UserId   int    `db:"user_id"`
    Username string `db:"username"`
    Sex      string `db:"sex"`
    Email    string `db:"email"`
}

type Place struct {
    Country string `db:"country"`
    City    string `db:"city"`
    TelCode int    `db:"telcode"`
}

var Db *sqlx.DB

func init() {
    //初始化鏈接數據庫
    database, err := sqlx.Open("mysql", "root@tcp(127.0.0.1:3306)/test")
    if err != nil {
        fmt.Println("open mysql failed,", err)
        return
    }
    Db = database
}

func main() {
    _, err := Db.Exec("delete FROM person where user_id=?", 1)
    if err != nil {
        fmt.Println("exec failed,", err)
        return
    }
    fmt.Println("delete succ")
}
delete

Golang之Mysql操作