mysql go命令行_第五单元 go操作mysql

一、标准包引用

二、实现增删改查功能

一、标准包引用

github.com/go-sql-driver/mysql (mysql驱动)

github.com/jmoiron/sqlx (基于mysql驱动的封装)

命令行输入 :

go get github.com/go-sql-driver/mysql

go get github.com/jmoiron/sqlx

二、实现增删改查功能

1.数据库连接

用法:database, err := sqlx.Open(数据库驱动, 用户名:密码@tcp(db地址:端口)/数据库名)

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

示例代码

// test

package main

import (

"fmt"

_ "github.com/go-sql-driver/mysql"

"github.com/jmoiron/sqlx"

)

func main() {

_, err := sqlx.Open("mysql", "root:root@tcp(127.0.0.1:3306)/1803a")

if err != nil {

fmt.Println("数据库连接失败", err.Error())

} else {

fmt.Println("数据库连接成功")

}

}

2.新建表结构

CREATE TABLE `person` (

`user_id` int(11) NOT NULL AUTO_INCREMENT,

`username` varchar(260) DEFAULT NULL,

`sex` varchar(260) DEFAULT NULL,

`email` varchar(260) DEFAULT NULL,

PRIMARY KEY (`user_id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

3.新增功能实现

type Person struct {

UserId int `db:"user_id"`

Username string `db:"username"`

Sex string `db:"sex"`

Email string `db:"email"`

}

var Db *sqlx.DB

func init() {

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

if err != nil {

fmt.Println("open mysql failed,", err)

return

}

Db = database

defer db.Close() // 注意这行代码要写在上面err判断的下面

}

func main() {

r, err := Db.Exec("insert into person(username, sex, email)values(?, ?, ?)", "stu001", "man", "stu01@qq.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)

}

4.查询实现

代码示例

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"`

}

var Db *sqlx.DB

func init() {

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

if err != nil {

fmt.Println("open mysql failed,", err)

return

}

Db = database

//defer Db.Close() // 注意这行代码要写在上面err判断的下面

}

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)

}

5.更新操作

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"`

}

var Db *sqlx.DB

func init() {

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

if err != nil {

fmt.Println("open mysql failed,", err)

return

}

Db = database

//defer Db.Close() // 注意这行代码要写在上面err判断的下面

}

func main() {

res, err := Db.Exec("update person set username=? where user_id=?", "stu0003", 1)

if err != nil {

fmt.Println("exec failed, ", err)

return

}

row, err := res.RowsAffected()

if err != nil {

fmt.Println("rows failed, ", err)

}

fmt.Println("update succ:", row)

}

6.删除操作

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"`

}

var Db *sqlx.DB

func init() {

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

if err != nil {

fmt.Println("open mysql failed,", err)

return

}

Db = database

//defer Db.Close() // 注意这行代码要写在上面err判断的下面

}

func main() {

res, err := Db.Exec("delete from person where user_id=?", 1)

if err != nil {

fmt.Println("exec failed, ", err)

return

}

row, err := res.RowsAffected()

if err != nil {

fmt.Println("rows failed, ", err)

}

fmt.Println("delete succ:", row)

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值