Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

go

I have a problem with getting database table list (SHOW TABLES) in Go.

I use this packages

database/sql

gopkg.in/gorp.v1

github.com/ziutek/mymysql/godrv

and connect to MYSQL by this code:

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!

like image 764
Amir Mohsen Avatar asked Dec 14 '22 23:12

Amir Mohsen


1 Answers

I use classic 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! This is only an example

like image 134
vladkras Avatar answered Feb 16 '23 01:02

vladkras