无法获取语句show full columns from admin某些字段?

sql.Scan() 对show full columns from admin 这条命令的处理有问题, 很奇怪的赋值.

package main

import (
    "database/sql"
    "strings"

    _ "github.com/go-sql-driver/mysql"
)

var (
    db *sql.DB
)

func main() {
    cc, err := sql.Open("mysql", "root:aaaaaaaa@tcp(127.0.0.1:3306)/99a?charset=utf8")
    if err = cc.Ping(); err != nil {
        panic(err)
    }
    db = cc
    print(getStruct("admin"))

}

func getStruct(tableName string) string {
    // 查询表信息;
    str := ""      //结果字符串
    var KEY string //主键
    rows, _ := db.Query("show full columns from `" + tableName + "`")
    for rows.Next() {
        //***
        //为什么这里的不到结果呢??????????????
        //***//
        Field, Type, Collation, Null, Key, Default, Extra, Privileges, Comment := "", "", "", "", "", "", "", "", ""
        rows.Scan(&Field, &Type, &Collation, &Null, &Key, &Default, &Extra, &Privileges, &Comment)
        strs := strings.SplitN(Type, "(", 2)

        if Null == "NO" {
            Null = " NOT NULL "
        } else {
            Null = ""
        }
        if Key == "PRI" { //竟然获取不了什么意思?
            KEY = Field
        }
        if Default != "" { //竟然获取不了什么意思?
            Default = " DEFAULT \"" + Default + "\""
        }
        if Comment != "" { //竟然只获得一个 ; 明明多行都有注释;
            Comment = "//" + Comment
        }

        str = str + " \r\n " + Field + " " + strs[0] + " `" + Type + Null + Default + "`" + Comment

    }
    str = KEY + "type " + strings.Title(tableName) + " struct {" + str + "\n}"

    return str
}

&Null, &Key, &Default, &Extra, &Privileges, &Comment

后面这些变量没有赋值, 到底是什么问题????

//下面的是真实表结构-------------//
CREATE TABLE `admin` (
  `uid` int(11) NOT NULL,
  `username` char(18) NOT NULL COMMENT '用户名',
  `password` char(32) NOT NULL COMMENT '密码',
  `mobile` int(11) DEFAULT '135555555',
  `info` varchar(255) DEFAULT '' COMMENT '信息',
  `other` text COMMENT '其他',
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
//--------------------------//
而函数结果却是:
type Admin struct { 
 uid int `int(11)` 
 username char `char(18) NOT NULL ` 
 password char `char(32) NOT NULL ` 
 mobile int `int(11)` 
 info varchar `varchar(255)`//信息 
 other text `text`
}

很多字段没有被rows.Scan读取并写到参数中;
求问如何解决?

共 2 个回复


jimmykuu

Admin 中定义的字段首字母没有大写,对外是不可见的,Scan 方法无法访问到。

# 0

aaa

那些都是 变量, 根本就不是字段;
一部分能获取, 一部分获取不了;
很奇怪.

show full columns from admin

# 1