简体   繁体   English

如何使用go语言获取数据库表列表(SHOW TABLES)

[英]How To get database Tables list in go language (SHOW TABLES)

I have a problem with getting database table list (SHOW TABLES) in go lang 我在go lang中获取数据库表列表(SHOW TABLES)时遇到问题

I use this packages 我用这个包

database/sql 数据库/ SQL

gopkg.in/gorp.v1 gopkg.in/gorp.v1

github.com/ziutek/mymysql/godrv github.com/ziutek/mymysql/godrv

and connect to MYSQL by this code: 并通过以下代码连接到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}}

And I use this code to get list of tables 我使用此代码获取表的列表

result, _ := dbmap.Exec("SHOW TABLES")

But result is empty! 但结果是空的!

I'm trying this code and work successfully. 我正在尝试使用此代码并成功运行。 I create a list of string and use Select query to get list of database tables. 我创建一个字符串列表并使用Select查询来获取数据库表的列表。

tables := []string{}
dbmap.Select(&tables, "SHOW TABLES")
fmt.Println(tables)

I use classic go-sql-driver/mysql : 我使用经典的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)
}

PS don't ignore errors! PS不要忽视错误! This is only an example 这只是一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM