Go 和 pgx:无法将 [+11111111111] 转换为文本
我是 golang 和 pgx 的新手,当我尝试运行简单的查询时遇到了问题。我在 postgres 中有下表。
CREATE TABLE users (
user_id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
phone_number TEXT NOT NULL UNIQUE,
);
当我尝试运行以下查询时:
func sampleFunc(db dbClient){
number := "+111111111"
rows, err := db.Query(context.Background(), "SELECT user_id FROM users WHERE phone_number=$1::TEXT;", number)
if err != nil {
log.Println(err)
return false, err
}
}
出现以下错误:无法将 [+111111111] 转换为文本。
编辑 03/05/2022
我应该注意,我将 pgxpool.Pool
包装在我自己的结构中,并且由于某种原因,当我使用 pgxpool 时实际上没有问题直接地。
type dbClient interface {
Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)
}
type SQLClient struct {
Conn *pgxpool.Pool
}
func (db *SQLClient) Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error) {
return db.Conn.Query(ctx, query, args)
}
我假设类型信息发生了一些问题,但仍然不知道如何修复它。
当我使用 fmt.Println(reflect.TypeOf(args))
在 SQLClient.Query
中打印 args
类型信息时,我得到了以下:[]界面{}
I'm new to golang and pgx and I'm running into an issue when I try to run a simple query. I have the following table in postgres.
CREATE TABLE users (
user_id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
phone_number TEXT NOT NULL UNIQUE,
);
When I try to run the following query:
func sampleFunc(db dbClient){
number := "+111111111"
rows, err := db.Query(context.Background(), "SELECT user_id FROM users WHERE phone_number=$1::TEXT;", number)
if err != nil {
log.Println(err)
return false, err
}
}
I get the following error: cannot convert [+111111111] to Text.
EDIT 03/05/2022
I should note that I'm wrapping the pgxpool.Pool
in my own struct, and for some reason there's actually no issue when I use the pgxpool directly.
type dbClient interface {
Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)
}
type SQLClient struct {
Conn *pgxpool.Pool
}
func (db *SQLClient) Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error) {
return db.Conn.Query(ctx, query, args)
}
I'm assuming that then something is happening with the type information, but still can't figure out how to go about fixing it.
When I print the args
type info in SQLClient.Query
, by using fmt.Println(reflect.TypeOf(args))
, I get the following: []interface {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您忘记将
...
添加到传递给db.Conn.Query()
的args
参数中。规范。
You forgot to add
...
to theargs
argument passed todb.Conn.Query()
.The spec.
我在这里看到了同样的问题
I saw the same problem here