0

执行SELECT查询并将结果存储在具有 type 字段的结构中时interface,结果类型会有所不同int64[]uint8具体取决于我使用的数据库是 MySQL 数据库还是 MariaDB。

这与我之前提出的一个问题(并得到了回答)有关:Go SQL query inconsistency

给定一个接口如下:

type Result struct {
    Afield string      `db:"A"`
    Bfield interface{} `db:"B"`
    Cfield string      `db:"C"`
    Dfield string      `db:"D"`
}

和数据库表:

A : VARCHAR(50)
B : INT
C : VARCHAR(50)
D : VARCHAR(50)

我要执行的查询:

从表中选择 A、B、C、D,其中 A="a"

它是如何执行的:

db.Get(&result, `SELECT A, B, C, D FROM table WHERE A=?`, "a")

问题

如果在 MySQL 数据库上执行,则类型Bfieldint64(desired),但如果在 MariaDB 上执行,则类型为[]uint8

为什么 Bfield 的类型不同,取决于查询是在 MySQL db 还是 MariaDB 上执行的?

可能有用的信息:

db 是 type: *sqlx.DB,然后传递给执行查询的函数,其中 db 是 type Connection

// Connection is an interface for making queries.
type Connection interface {
    Exec(query string, args ...interface{}) (sql.Result, error)
    Get(dest interface{}, query string, args ...interface{}) error
    Select(dest interface{}, query string, args ...interface{}) error
}

我正在使用https://github.com/jmoiron/sqlx

4

0 回答 0