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

mysql

时间:2018-03-10 19:30:27      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:strong   art   127.0.0.1   lis   exec   pos   alt   nan   package   

1、mysql编程 

  a. 安装sqlx 

    go get github.com/jmoiron/sqlx 

  b. 链接mysql

    database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")

2、mysql相关操作

insert操作
r, err := Db.Exec("insert into person(username, sex, email)values(?, ?, ?)", "stu001", "man", "stu01@qq.com")

Select 操作
err := Db.Select(&person, "select user_id, username, sex, email from person where user_id=?", 1)

update操作
_, err := Db.Exec("update person set username=? where user_id=?", "stu0001", 1)

Delete 操作
_, err := Db.Exec("delete from person where user_id=?", 1)

插入:

技术分享图片
import (
    "fmt"
    "time"
    _ "github.com/go-sql-driver/mysql" //mysql驱动实例注册,在init注册到go的接口里面去
    "github.com/jmoiron/sqlx"
    "math/rand"
)

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

func main() {
    Db, err := sqlx.Open("mysql", "root:@tcp(10.0.0.200:3306)/godb")
    if err != nil {
        fmt.Println("connect to msyql failed, ", err)
        return
    }

    startTime := time.Now().UnixNano()
    
    fmt.Println("connect to mysql succ")    
    username := fmt.Sprintf("user%d", rand.Int63())
    email := fmt.Sprintf("%d@qq.com", rand.Int())
    sex := ""
    //插入数据
    result, err := Db.Exec("insert into user_info(username, sex, email)values(?,?,?)",
    username, sex, email)
    if err != nil {
        fmt.Println("insert failed, err:", err)
        return
    }
    user_id, _ := result.LastInsertId()    //返回插入的行的id
    fmt.Println("insert succ, user_id:", user_id)
    endTime := time.Now().UnixNano()
    fmt.Printf("insert succ cost:%d ms\n", (endTime-startTime)/1000/1000)
    Db.Close()
}
View Code

查询:

技术分享图片
package main

import (
    "fmt"
    "time"
    _ "github.com/go-sql-driver/mysql" //mysql驱动实例注册,在init注册到go的接口里面去
    "github.com/jmoiron/sqlx"    
)

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

func main() {
    Db, err := sqlx.Open("mysql", "root:@tcp(10.0.0.200:3306)/godb")
    if err != nil {
        fmt.Println("connect to msyql failed, ", err)
        return
    }

    startTime := time.Now().UnixNano()    
    var userInfo UserInfo
    //查询一条语句
    err = Db.Get(&userInfo, "select user_id, username, sex, email from user_info where username=?", "user1674879938132494608")
    if err != nil {
        fmt.Println("get failed, err:", err)
        return
    }
    fmt.Printf("userInfo:%#v\n", userInfo)
    endTime := time.Now().UnixNano()
    fmt.Printf("insert succ cost:%d ms\n", (endTime-startTime)/1000/1000)
    
    
    
    var userInfoList []*UserInfo
    //查询多条语句
    err = Db.Select(&userInfoList, "select user_id, username, sex, email from user_info where user_id>?", 2)

    fmt.Printf("user_info_list:%#v\n", userInfoList)
    if err != nil {
        fmt.Println("select failed, err:", err)
        return
    }
    Db.Close()
}
View Code

数据库的原子操作:

技术分享图片
package main

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

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

func main() {
    Db, err := sqlx.Open("mysql", "root:@tcp(10.0.0.200:3306)/godb")
    if err != nil {
        fmt.Println("connect to msyql failed, ", err)
        return
    }

    startTime := time.Now().UnixNano()
    conn, _ := Db.Begin() //原子操作

    _, err = conn.Exec("insert into user_info(username, sex, email)values(?,?,?)",
        "user01", "man", "email")
    if err != nil {
        conn.Rollback() //回滚
        fmt.Println("insert failed, err:", err)
        return
    }

    _, err = conn.Exec("insert into user_info(username, sex, email)values(?,?,?)",
        "user01", "man", "email")
    if err != nil {
        conn.Rollback() //回滚
        fmt.Println("insert failed, err:", err)
        return
    }

    conn.Commit()
    Db.Close()
}
View Code

 

mysql

标签:strong   art   127.0.0.1   lis   exec   pos   alt   nan   package   

原文地址:https://www.cnblogs.com/domestique/p/8541512.html

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