Go语言Mysql简单使用.
查询: package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" "time" ) func main() { con, _ := sql.Open("mysql", "root:123456@tcp(localhost:3316)/mysql") defer con.Close() go query(con) time.Sleep(1e9) } func query(con *sql.DB) { Q, _ := con.Prepare("select user,host,password from user where user = ? and host = ?") defer Q.Close() var x, y, z string defer func() { if e := recover(); e != nil { fmt.Println(e) } }() d, _ := Q.Query("root", "127.0.0.1") for d.Next() { d.Scan(&x, &y, &z) fmt.Println(z, y, z) } }
新建:
package main import ( "database/sql" //"fmt" _ "github.com/go-sql-driver/mysql" //"time" ) func main() { con, _ := sql.Open("mysql", "root:123456@tcp(localhost:3316)/test") defer con.Close() P, _ := con.Prepare("CREATE TABLE Test (id int not null AUTO_INCREMENT PRIMARY KEY,name varchar(20) not null)") defer P.Close() P.Exec() }