go语言取mysql数据。之前的测试并没有成功,也没有找到原因,但是今天编译了一下又成功了,具体修改了哪里我也忘了。赶紧做个记录。敏感信息已经隐藏
获得go语言的mysql驱动
go get code.google.com/p/go-mysql-driver/mysql
也可以
驱动安装
go get github.com/go-sql-driver/mysql
go install github.com/go-sql-driver/mysql
2013-5-27修正:该mysql驱动被移到git以后使用“$ go get github.com/go-sql-driver/mysql”
项目里调用为“import _ “github.com/go-sql-driver/mysql””
源码如下:
package main import ("database/sql" _ "code.google.com/p/go-mysql-driver/mysql" "fmt") func main() { db, e := sql.Open("mysql", "simon:zhang@tcp(127.0.0.1:3306)/simonzhang?charset=utf8") if e != nil { print("ERROR") return } println("Conn DB OK") rows, e := db.Query("select user,passwd from user") if e != nil { fmt.Print("error:%v\n", e) return } if rows == nil { print("Rows is nil") return } for rows.Next() { var u,p string err := rows.Scan(&u,&p) if err != nil { print("Row error!") return } fmt.Println(u," ",p) } println("WIN!") }
运行结果
只有更新速度够快,俺们这些粉丝才能更忠实!