Go語言Mysql簡單使用.
查詢:
package mainimport ( "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) } } </pre>
新建:package mainimport ( "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() } </pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!