go對資料庫的基本操作
阿新 • • 發佈:2018-12-12
package main
import(
“github.com/jmoiron/sqlx”
_ “github.com/go-sql-driver/mysql”
)
func main(){
database, err := sqlx.Open(“mysql”, “root:[email protected](127.0.0.1:3306)/logadmin”)//格式 (使用者名稱:密碼@tcp(ip:埠號)/資料庫名)
r, err := Db.Exec(“insert into person(username, sex, email)values(?, ?, ?)”, “stu001”, “man”, "[email protected]")//插入 person為logadmin資料庫裡的表名
err := Db.Select(&person, “select user_id, username, sex, email from person where user_id=?”, 1) //查詢
_, err := Db.Exec(“update person set username=? where user_id=?”, “stu0003”, 1)//更新
_, err := Db.Exec(“delete from person where user_id=?”, 1) //刪除
}