使用Golang从MYSQL打印表名

huangapple go评论72阅读模式
英文:

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"`
}

huangapple
  • 本文由 发表于 2022年4月6日 18:59:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/71765554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定