英文:
Printing table name from MYSQL using Golang
问题
包 main
导入 (
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
类型标签 struct {
表名 string json:"table_name"
}
主函数() {
db, err := sql.Open("mysql", "root:roadrunner@tcp(172.31.16.82:10002)/quote_data")
if err != nil {
//panic.(err.Error())
fmt.Println("连接失败")
}
fmt.Println("已连接")
defer db.Close()
结果,err1 := db.Query("SELECT table_name FROM information_schema.tables WHERE table_type = 'base table' AND table_schema='quote_data'")
if err1 != nil {
panic(err1.Error())
}
fmt.Println("显示表格",result)
for 结果.Next() {
var 标签 Tag
err1 = 结果.Scan(&tag.Table_name)
if err1 != nil {
panic(err.Error())
}
log.Printf(tag.Table_name)
}
}
英文:
package main
import (
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type Tag struct {
Table_name string `json:"table_name"`
}
func main() {
db, err := sql.Open("mysql", "root:roadrunner@tcp(172.31.16.82:10002)/quote_data")
if err != nil {
//panic.(err.Error())
fmt.Println("Connected Fail")
}
fmt.Println("Connected")
defer db.Close()
result, err1 := db.Query("SELECT table_name FROM information_schema.tables WHERE table_type = 'base table' AND table_schema='quote_data'")
if err1 != nil {
panic(err1.Error())
}
fmt.Println("Showing Tables",result)
for results.Next() {
var tag Tag
err1 = results.Scan(&tag.Table_name)
if err1 != nil {
panic(err.Error())
}
log.Printf(tag.Table_name)
}
}
答案1
得分: 2
你必须添加类型为db的table_name来初始化数据库表
type Tag struct {
Table_name string `db:"table_name" json:"table_name"`
}
英文:
you must add type db with table_name to initialize database table
type Tag struct {
Table_name string `db:"table_name" json:"table_name"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论