我正在尝试使用准备好的语句从Post命题表中获取一些数据。
如果我尝试使用database.Get(),所有内容都会返回。
表:
create table accounts
(
id bigserial not null
constraint accounts_pkey
primary key,
identificator text not null,
password text not null,
salt text not null,
type smallint not null,
level smallint not null,
created_at timestamp not null,
updated timestamp not null,
expiry_date timestamp,
qr_key text
);账户结构:
type Account struct {
ID string `db:"id"`
Identificator string `db:"identificator"`
Password string `db:"password"`
Salt string `db:"salt"`
Type int `db:"type"`
Level int `db:"level"`
ExpiryDate time.Time `db:"expiry_date"`
CreatedAt time.Time `db:"created_at"`
UpdateAt time.Time `db:"updated_at"`
QrKey sql.NullString `db:"qr_key"`
}顺便说一下我试过用?而不是1美元和2美元
stmt, err := database.Preparex(`SELECT * FROM accounts where identificator = $1 and type = $2`)
if err != nil {
panic(err)
}
accounts := []account.Account{}
err = stmt.Get(&accounts, "asd", 123)
if err != nil {
panic(err)
}我得到的错误是
"errorMessage":“结果为\u003e1列(10)的可扫描类型片”,
在表中,除了帐户(struct)中的ID外,我没有尝试删除所有字段的记录,但是它不起作用。
发布于 2018-05-05 08:05:54
sqlx的文档将获取和选择描述为:
Get和Select在可扫描类型上使用rows.Scan,在不可扫描类型上使用rows.StructScan .它们大致类似于QueryRow和Query,其中Get对于获取单个结果并扫描它很有用,而Select用于获取结果的片段:
要获取单个记录,请使用Get。
stmt, err := database.Preparex(`SELECT * FROM accounts where identificator = $1 and type = $2`)
var account Account
err = stmt.Get(&account, "asd", 123)如果查询返回多条记录,请使用Select with语句如下:
stmt, err := database.Preparex(`SELECT * FROM accounts where identificator = $1 and type = $2`)
var accounts []Account
err = stmt.Select(&accounts, "asd", 123)在您的情况下,如果您使用stmt.Select而不是stmt.Get。看起来不错。
https://stackoverflow.com/questions/50187083
复制相似问题