我在 go lang 中获取数据库表列表 (SHOW TABLES) 时遇到问题
我用这个包
数据库/sql
gopkg.in/gorp.v1
github.com/ziutek/mymysql/godrv
并通过以下代码连接到 MYSQL:
db, err := sql.Open(
"mymysql",
"tcp:127.0.0.1:3306*test/root/root")
if err != nil {
panic(err)
}
dbmap := &DbMap{Conn:&gorp.DbMap{Db: db}}
Run Code Online (Sandbox Code Playgroud)
我使用此代码获取表格列表
result, _ := dbmap.Exec("SHOW TABLES")
Run Code Online (Sandbox Code Playgroud)
但结果是空的!
我使用经典的go-sql-driver/mysql:
db, _ := sql.Open("mysql", "root:qwerty@/dbname")
res, _ := db.Query("SHOW TABLES")
var table string
for res.Next() {
res.Scan(&table)
fmt.Println(table)
}
Run Code Online (Sandbox Code Playgroud)
PS 不要忽略错误!这只是一个例子