码迷,mamicode.com
首页 > 数据库 > 详细

Golang之Mysql操作

时间:2018-01-26 17:04:12      阅读:1573      评论:0      收藏:0      [点我收藏+]

标签:操作   账号   where   eve   增加   36-6   int   ret   failed   

话说当年武大郎对着电脑一顿噼里啪啦,,,对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", "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)
}
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操作

标签:操作   账号   where   eve   增加   36-6   int   ret   failed   

原文地址:https://www.cnblogs.com/pyyu/p/8360069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!