Go语言中操作MySQL数据库的方法


当前第2页 返回上一页

单行查询:

1

2

3

4

row:= conn.QueryRow(`select * from user where userName = "wyj"`)

var name,pwd string

row.Scan(&name,&pwd)

beego.Info(name,"------",pwd)

多行查询:

1

2

3

4

5

6

7

8

data ,err :=conn.Query("SELECT name from user")

    var userName string

    if err == nil{

        for data.Next(){

            data.Scan(&userName)

            beego.Info(userName)

        }

    }

全部代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

//连接数据库

conn,err := sql.Open("mysql","root:123456@tcp(127.0.0.1:3306)/testtest?charset=utf8")

    if err != nil{

        beego.Info("链接失败")

    }

    defer conn.Close()

//建表

    res ,err:= conn.Exec("create table user(userName VARCHAR(40),passwd VARCHAR(40))")

    beego.Info("create table result=",res,err)

//插入数据

    res,err =conn.Exec("insert user(userName,passwd) values(?,?)","itcast","heima")

    beego.Info(res,err)

//单行查询

    row:= conn.QueryRow(`select * from user where userName = "wyj"`)

    var name,pwd string

    row.Scan(&name,&pwd)

    beego.Info(name,"------",pwd)

//多行查询

    data ,err :=conn.Query("SELECT userName from user")

    var userName string

    if err == nil{

        for data.Next(){

            data.Scan(&userName)

            beego.Error(userName)

        }

    }

更多go语言知识请关注php中文网go语言教程栏目。

以上就是Go语言中操作MySQL数据库的方法的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

Go语言切片(slice)

Go语言指针作为函数参数

怎样下载安装Go语言

mac下怎么安装Go语言

Go语言导包时“.”和“_”的区别是什么?

Go语言数据类型转换教程

idea如何配置运行Go语言的环境

Go语言查看环境信息的方法

Go语言int转string的方法

macos用什么写Go语言

更多相关阅读请进入《Go语言》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...