像python一样使用golang操作mysql
作为一个DBA,平时工作中经常需要使用python和go对数据库操作,但是在使用go的时候总觉得很不丝滑,不能像python一样通过纯SQL完成操作,没办法只好造轮子了...
Installation
$ go get -u github.com/jingmingyu/dbsdk
Usage
mysqldb := db_mysql.MYSQL{
Host: "127.0.0.1",
Port: "3306",
Username: "test",
Password: "test",
Dbname: "mytestdb",
}
Select Method:
SelectToJson
SelectToRowsData
SelectToJson : Ordinary select like select / show master status
querysql := fmt.Sprintf("%s", "SELECT * FROM `dbtool_accesslog`")
res, _ := mysqldb.SelectToJson(querysql)
fmt.Println(res)
result :
[{
"created_by": {
"String": "",
"Valid": false
},
"id": {
"String": "10",
"Valid": true
},
"operate_page": {
"String": "abc",
"Valid": true
},
"when_created": {
"String": "2022-03-17T22:37:23+08:00",
"Valid": true
}
}]
SelectToRowsData: show engine innodb status
querysql := fmt.Sprintf("%s", "show engine innodb status")
res, _ := mysqldb.SelectToRowsData(querysql)
fmt.Println(res.Data)
res:
[[{InnoDB true} { true} {
=====================================
2022-03-18 10:04:00 140672645170944 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 59 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 5073576 srv_active, 0 srv_shutdown, 1710 srv_idle
srv_master_thread log flush and writes: 0
----------
...
DML Method:
insert
insertsql := fmt.Sprintf("%s", "insert into `dbtool_accesslog`( `operate_page` , `when_created` ) VALUE ('zzzzzzzz',now())")
res, _ := mysqldb.DirectExec(insertsql)
fmt.Println(res)
delete
deletesql := fmt.Sprintf("%s", "delete from `dbtool_accesslog`")
res, _ := mysqldb.DirectExec(deletesql)
fmt.Println(res)
update
updatesql := fmt.Sprintf("%s", "update `dbtool_accesslog` set operate_page='ascd' ")
res, _ := mysqldb.DirectExec(updatesql)
fmt.Println(res)
TRX Exec Method:
DirectExec
updatesql := fmt.Sprintf("%s", "update `dbtool_accesslog` set operate_page='ascd' ")
res, _ := mysqldb.DirectExec(updatesql)
fmt.Println(res)
exec add/drop ddl
addsql := fmt.Sprintf("%s", "alter table dbtool_accesslog add zzz int(2)")
res, _ := mysqldb.DirectExec(addsql)
fmt.Println(res)
dropsql := fmt.Sprintf("%s", "alter table dbtool_accesslog drop COLUMN zzz ")
res, _ := mysqldb.DirectExec(dropsql)
fmt.Println(res)
SingleTrxExec
//single trx exec
// 单个事务执行
trxsql := fmt.Sprintf("%s", "insert into `dbtool_accesslog`( `operate_page` , `when_created` ) VALUE ('zzzzzzzz',now())")
res, err := mysqldb.SingleTrxExec(trxsql)
if err != nil {
fmt.Println(err)
}
fmt.Println(res)
ComTrxExec
// combine trx exec
// 组合事务顺序执行
var trx []string
trxsql1 := fmt.Sprintf("%s", "insert into `dbtool_accesslog`( `operate_page` , `when_created` ) VALUE ('zzzzzzzz',now())")
trxsql2 := fmt.Sprintf("%s", "insert into `dbtool_accesslog`( `operate_page` , `when_created` ) VALUE ('bbbbb',now())")
trxsql3 := fmt.Sprintf("%s", "insert into `dbtool_accesslog`( `operate_page` , `when_created` ) VALUE ('bbbbb',now())")
trxsql4 := fmt.Sprintf("%s", "update `dbtool_accesslog` set operate_page='zzzzzzzz' where operate_page='ascd'")
trxsql5 := fmt.Sprintf("%s", "insert into `dbtool_accesslog`( `operate_page` , `when_created` ) VALUE ('zzzzzzzz',now())")
trx = append(trx,trxsql1,trxsql2,trxsql3,trxsql4,trxsql5)
res, err := mysqldb.ComTrxExec(trx)
if err != nil {
fmt.Println(err)
}
for _,v := range res{
fmt.Println(v)
}