搭好GO开发环境后,获取mysql开发驱动包:
go get code.google.com/p/go-mysql-driver/mysql
之后就可以使用MySQL了
package main
import "fmt"
import "database/sql"
import "code.google.com/p/go-mysql-driver/mysql"
func main(){
fmt.Printf("query:\n")
db,e := sql.Open("mysql", "root:@/mysql?charset=utf8")
//说明:第一个参数"mysql"表示打开一个MYSQL链接
//第二个参数是一个DSN,格式是:
//[username[:password]@][protocol[(address)]]/dbname[?param1=value1¶mN=valueN]
if e != nil {
fmt.Printf("conn error.");
return;
}
rows,e := db.Query("select host,user from user")
if e != nil {
fmt.Printf("query error.");
return;
}
i := 0
for rows.Next() {
i++
var ss,s2 string
e = rows.Scan( &ss,&s2 )
if e == nil {
fmt.Printf("Title: %s,%s \n", ss,s2 )
}else{
fmt.Printf("error")
}
}
fmt.Printf("Total: %d\n" , i )
db.Close()
}