Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql Sqlx使用准备好的语句获取_Postgresql_Go_Sqlx - Fatal编程技术网

Postgresql Sqlx使用准备好的语句获取

Postgresql Sqlx使用准备好的语句获取,postgresql,go,sqlx,Postgresql,Go,Sqlx,我试图使用准备好的语句从postgress表中获取一些数据 如果我尝试使用database.Get(),则返回所有内容 表: create table accounts ( id bigserial not null constraint accounts_pkey primary key, identificator text not null, password text not null, salt

我试图使用准备好的语句从postgress表中获取一些数据

如果我尝试使用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以外的所有字段的记录,但是它不起作用。

sqlx文档描述如下:

获取
选择
使用行。扫描可扫描类型和行。结构扫描打开 不可扫描类型。他们对询问和质询的态度大致相同, 其中Get对于获取单个结果并对其进行扫描非常有用,以及 选择对于获取结果片段很有用:

要获取单个记录,请使用
Get

stmt, err := database.Preparex(`SELECT * FROM accounts where identificator = $1 and type = $2`)
var account Account
err = stmt.Get(&account, "asd", 123)
如果查询返回多条记录,请使用
选择
,语句如下:

stmt, err := database.Preparex(`SELECT * FROM accounts where identificator = $1 and type = $2`)
var accounts []Account
err = stmt.Select(&accounts, "asd", 123)

在您的情况下,如果您使用
stmt.Get
,请选择
。它将起作用。

在pgadmin中直接在prepare中运行查询。检查它返回多少行或返回一行。因为Get用于选择单个记录。它对我们有效,但我们使用了
而不是
$