Golang之Mysql操作

2018-08-31 15:54:00 浏览数 (1)

话说当年武大郎对着电脑一顿噼里啪啦,,,对mysql增删改查

增加insert

代码语言:javascript复制
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", "yc_uuu@163.com")
    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)
}

查询select

代码语言:javascript复制
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)

}

改update

代码语言:javascript复制
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)
    }
}

删delete

代码语言:javascript复制
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")
}

0 人点赞