美文网首页
golang-MySql

golang-MySql

作者: liziNo1 | 来源:发表于2021-08-12 17:52 被阅读0次

一、mySql连接

1、引入驱动包和xorm

import "github.com/go-xorm/xorm"

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

2、连接数据库

engine,err:=xorm.NewEngine("mysql","user:pwd@tcp(127.0.0.1:3306)/db_name?charset=utf8")

if config.ENV !=config.RELEASE{

engine.ShowSQL(true)

}

}

3、查询

db.QueryString("select *from tbl_name where id=? and user_name=?",id,userName)

db.QueryInterface("select *from tbl_name where id=? and user_name=?",id,userName)

4、插入

此处插入用其他方式会报错,不知为何

insertSQl :=fmt.Sprintf("insert into tbl_name(user_name,id) values('%s','%s')",userName,id)

_,err := db.Exec(insertSQl)

5、删除

此处插入用其他方式会报错,不知为何

deleteSql :=fmt.Sprintf("delete from tbl_name where id='%s' ",id)

_, err := db.Exec(deleteSql)

5、存储过程

sql1 :=fmt.Sprintf("EXEC tbl_name_XX @id=?,@status=?,@msg=?")

res1,err1 := sqlEngine.QueryInterface(sql1,id,0,"")

相关文章

  • golang-MySql

    一、mySql连接 1、引入驱动包和xorm import "github.com/go-xorm/xorm" i...

网友评论

      本文标题:golang-MySql

      本文链接:https://www.haomeiwen.com/subject/gcnlbltx.html