??????????????????????database/sql??????????????????????
package main
import (
_ "github.com/Go-SQL-Driver/MySQL"
"database/sql"
"fmt"
//"time"
)
func main() {
db?? err := sql.Open("mysql"?? "astaxie:astaxie@/test?charset=utf8")
checkErr(err)
//????????
stmt?? err := db.Prepare("INSERT userinfo SET username=???departname=???created=?")
checkErr(err)
res?? err := stmt.Exec("astaxie"?? "?з?????"?? "2012-12-09")
checkErr(err)
id?? err := res.LastInsertId()
checkErr(err)
fmt.Println(id)
//????????
stmt?? err = db.Prepare("update userinfo set username=? where uid=?")
checkErr(err)
res?? err = stmt.Exec("astaxieupdate"?? id)
checkErr(err)
affect?? err := res.RowsAffected()
checkErr(err)
fmt.Println(affect)
//???????
rows?? err := db.Query("SELECT * FROM userinfo")
checkErr(err)
for rows.Next() {
var uid int
var username string
var department string
var created string
err = rows.Scan(&uid?? &username?? &department?? &created)
checkErr(err)
fmt.Println(uid)
fmt.Println(username)
fmt.Println(department)
fmt.Println(created)
}
//???????
stmt?? err = db.Prepare("delete from userinfo where uid=?")
checkErr(err)
res?? err = stmt.Exec(id)
checkErr(err)
affect?? err = res.RowsAffected()
checkErr(err)
fmt.Println(affect)
db.Close()
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
???????????????????????????Go????Mysql?????????????
???????????????????????£?
????sql.Open()??????????????????????????????Go-MySQL-Driver???????mysql?????????????????????????DNS(Data Source Name)??????Go-MySQL-Driver??????Щ????????????????????????????????
????user@unix(/path/to/socket)/dbname?charset=utf8
????user:password@tcp(localhost:5555)/dbname?charset=utf8
????user:password@/dbname
????user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname
????db.Prepare()??????????????????е?sql?????????????????????????
????db.Query()??????????????Sql????Rows?????
????stmt.Exec()???????????stmt??????SQL???
?????????????????????????????=???????????????????????????????????SQL???