Golang之Mysql操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang之Mysql操作相关的知识,希望对你有一定的参考价值。

话说当年武大郎对着电脑一顿噼里啪啦,,,对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", "[email protected](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", "[email protected](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", "[email protected](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操作的主要内容,如果未能解决你的问题,请参考以下文章

golang 碎片整理之MySQL

Golang之mysql

golang goroutine例子[golang并发代码片段]

golang代码片段(摘抄)

golang 之 sql

代码片段 - Golang 实现简单的 Web 服务器