xorm执行sql语句

package main

import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
)

var (
engine *xorm.Engine
)

func main() {
//数据库连接参数
params := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true", "root", "root", "localhost:3306", "go")
var err error
//连接数据库
engine, err = xorm.NewEngine("mysql", params)
if err != nil {
panic(err)
}

//执行sql查询,返回数据格式:[]map[string][]byte
gsql := "SELECT `member_id`, `member_name` FROM `go_member`"
gres, gerr := engine.Query(gsql)
if gerr != nil {
panic(gerr)
}
for _, v := range gres {
fmt.Printf("信息ID: %s, 姓名: %s\n", string(v["member_id"]), string(v["member_name"]))
}

//执行sql命令(Insert/Update/Delete)
esql := "Update `go_member` SET `member_name` = ? WHERE `member_id` = ?"
eres, eerr := engine.Exec(esql, "tom", 4)
if eerr != nil {
panic(eerr)
}
num, ererr := eres.RowsAffected()
if ererr != nil {
panic(ererr)
}
fmt.Printf("总共修改了 %d 条数据\n", num)
}

 

【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://www.cnblogs.com/facetwitter/p/7595542.html

 

转载于:https://www.cnblogs.com/facetwitter/p/7595542.html

你可能感兴趣的:(xorm执行sql语句)