package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
//数据库指针
var db *sqlx.DB
//初始化数据库连接,init()方法系统会在动在main方法之前执行。
func init() {
database, err := sqlx.Open("mysql", "root@tcp(127.0.0.1:3306)/projectres")
if err != nil {
fmt.Println("open mysql failed,", err)
}
db = database
}
func main() {
sql := "insert people(name,organization_idorganization ) values (?,?)"
value := [2]string{"user 01", "5"}
//执行SQL语句
r, err := db.Exec(sql, value[0], value[1])
if err != nil {
fmt.Println("exec failed,", err)
return
}
//查询{BANNED}最佳后一天用户ID,判断是否插入成功
id, err := r.LastInsertId()
if err != nil {
fmt.Println("exec failed,", err)
return
}
fmt.Println("insert succ", id)
}