sqlplus是一个Golangdatabase/sql的通用扩展。
安装
gogetgithub.com/blusewang/sqlplus文档
详细文档,请前往 https://godoc.org/github.com/blusewang/sqlplus.
使用
typeTestObjstruct{Idstring`json:"id"`UserNamestring`json:"user_name"`}db,err:=sqlplus.Open("postgres","dsn...")iferr!=nil{log.Fatal(err)}//查列表varlist[]TestObjerr=db.QuerySlice(&list,"select*fromtest_tablewhereid<$1",100)iferr!=nil{log.Fatal(err)}log.Pringln(list)//[{3,""},{4,""}]//查单行varobjTestObjerr=db.QueryStruct(&obj,"select*fromtest_tablewhereid=$1",3)iferr!=nil{log.Fatal(err)}log.Pringln(obj)//{3,""}//事务tx,err:=db.Begin()err=tx.QueryStruct(&obj,"select*fromtest_tablewhereid=$1",3)iferr!=nil{_=tx.Rollback()log.Fatal(err)}else{_=tx.Commit()}log.Pringln(obj)//{3,""}
评论