mysql

Posted 关灯吃面

tags:

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

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", "[email protected]")

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("%[email protected]", 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的主要内容,如果未能解决你的问题,请参考以下文章

从mysql的片段中加载ListView

连接MySQL出现错误:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)(代码片段

使用 json rereiver php mysql 在片段中填充列表视图

关于mysql驱动版本报错解决,Cause: com.mysql.jdbc.exceptions.jdbc4Unknown system variable ‘query_cache_size(代码片段

修改MySQL密码报错“ERROR 1819 (HY000): Your password does not satisfy the current policy requirements“(代码片段

mysql查看版本的四种方法