- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试使用准备好的语句从 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": "scannable dest type slice with \u003e1 columns (10) in result",
在表中没有记录我试图从帐户(结构)中删除除 ID 之外的所有字段,但是它不起作用。
最佳答案
描述了 sqlx 的文档 Get and Select作为:
Get
andSelect
use rows.Scan on scannable types and rows.StructScan on non-scannable types. They are roughly analagous to QueryRow and Query, where Get is useful for fetching a single result and scanning it, and Select is useful for fetching a slice of results:
要获取单个记录,请使用 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 statement as:
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
。它会起作用。
关于postgresql - Sqlx 获取准备好的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50187083/
我正在使用 jmoiron sqlx我的 golang 项目的库。我试图创建一个数据库连接mysql。所以,我找到了这两个函数:sqlx.Connect() 和 sqlx.Open(),但没有发现区别
我正在尝试实现一个 sql 查询,它以 jQuery Datatables 期望的方式返回数据,同时允许用户自定义查询(我正在做很多输入验证以确保没有任何 sql 注入(inject))。查询是应该按
前言 上次咱们学习了如何使用Go操作Mysql,并且实现了简单的增删改查。 但是相对来说,还有有点复杂的,可能那些大佬也都觉得繁琐叭。 就又开发出了增强版查询Mysql操作库Sqlx。
我有不同类型的 REST API,它们有一些共同点: 所有类型都有Id 所有类型都有类型 所有类型都有属性,但属性不同 因为我不想有大量不必要的类型,所以我提出了一种类型格式,但它似乎不能被 sqlx
如果我尝试如下使用SQLx Rust的postgres功能, [dependencies] sqlx = { version = "0.3.3", default-features=false , f
我正在尝试使用准备好的语句从 postgress 表中获取一些数据 如果我尝试使用 database.Get() 返回所有内容。 表格: create table accounts ( id
假设我有两个模型, type Customer struct { Id int `json:"id" db:"id"` Name strin
main.rs: #[async_std::main] async fn main() -> Result { use sqlx::Connect; let mut conn = sq
在 sqlx有一个 Transaction type 允许您在一个事务中运行多个查询。 我试图弄清楚如何做到这一点,遗憾的是没有记录,尽管有自动生成的 API 文档。 我的第一次尝试: async f
我在MySQL数据库中有一张大表,我正试图尽可能高效地读取该表。我当时在考虑通过添加多个工作程序来加速代码,但是当我这样做时,在运行它的开始(并且仅在开始时)出现编码(marshal)处理错误,它看起
我在 Go 中使用 sqlx,这非常有帮助,但是当我使用 struct scan 并且 struct 的类型与 sql 类型不匹配时,它似乎没有抛出错误。例如,我在这里设置了一个数据库,使其具有名称(
我正在使用 sqlx 在我的 Go 代码中执行查询。该查询有一长串值(~10,000),我需要在 WHERE IN (?) 子句中对其进行过滤。这会导致性能大幅下降。我该如何优化这样的查询?此外,此查
此问题特定于 github.com/jmoiron/sqlx .我想用命名查询插入一个已知的时间戳或日期。我怎么做?我使用 time.Now() 作为给定时间戳的示例。 这是我在 MySQL 中的表:
我正在尝试使用一个简单的查询将两个表 person 和 profile 内部连接起来,这似乎适用于 mysql 但不适用于 sqlx。这是我的代码: package main import (
我正在使用 go 1.10.3,我正在尝试使用 sqlx 包获取一行并将其输入到带有 Get() 的结构中,或者获取多行并输入它们使用 Select() slice 。 让我们从将一行放入结构开始。
我想在单个查询中更新多行: //threadIDs is a variable length slice of integers like [3 5 6] query := "UPDATE messa
考虑以下示例: package main import ( "fmt" "github.com/jmoiron/sqlx" _ "github.com/go-sql-drive
但是下面粘贴的代码永远不起作用,而不是一个一个地迭代和扫描结果,是否有可能让下面的代码起作用?? 非常感谢您的投入。谢谢 type CustomData struct { ID
我正在尝试执行包含自定义 geoPoint 类型的 postgres 查询,但收到意外的 EOF 错误。对我做错了什么有什么想法吗? type Account struct { Id
我正在学习 Go 并尝试创建一个具有“字段”参数的 API 端点。当我尝试将 sqlx 结果行扫描到结构中时,但是用户省略的字段将作为空字符串返回。有没有办法可以更改结构以仅反射(reflect)用户
我是一名优秀的程序员,十分优秀!