使用 postgres 驱动程序在 golang 中执行带有参数的查询时出错
Error executing query with parameters in golang with postgres driver
我创建了一个在 postgres 数据库上执行查询的函数。我用驱动github.com/lib/pq
但是如果我 运行 这个:
_, err := repository.db.ExecContext(ctx, query, args...)
查询所在
INSERT INTO fakeclients (uuid, name, last_name, birth_day, email, city, address, password, cellphone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
并且 args 是具有此值及其类型的切片
f1571b24-d88e-42b3-907e-13f46efabacc is a string
myname is a string
lastname is a string
2020-12-12 is a string
email is a string
Cali is a string
Calle is a string
mypassword is a string
12334455es is a string
table 是使用此属性创建的
create table if not exists fakeclients (
id text,
corporate_account boolean,
name text,
last_name text,
cellphone text,
birth_day text,
email text,
password text,
city text,
address text,
primary key (id)
);
执行此查询的结果是
pq: syntax error at or near ","
但是如果我像这样在查询字符串中插入包含所有值的查询,它就可以工作
INSERT INTO fakeclients (uuid, name, last_name, birth_day, email, city, address, password, cellphone) VALUES ('f1571b24-d88e-42b3-907e-13f46efabacc is', 'myname', 'lastname', '2020-12-12', 'email', 'Cali', 'Calle', 'mypassword', '12334455es')
看起来 ExecContext 方法没有按预期解析参数
已解决,对于 postgres,插值查询的正确用法是使用 $1、$2、$3...
所以正确的查询应该是
INSERT INTO fakeclients (uuid, name, last_name, birth_day, email, city, address, password, cellphone) VALUES (, , , , , , , , )
我创建了一个在 postgres 数据库上执行查询的函数。我用驱动github.com/lib/pq
但是如果我 运行 这个:
_, err := repository.db.ExecContext(ctx, query, args...)
查询所在
INSERT INTO fakeclients (uuid, name, last_name, birth_day, email, city, address, password, cellphone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
并且 args 是具有此值及其类型的切片
f1571b24-d88e-42b3-907e-13f46efabacc is a string
myname is a string
lastname is a string
2020-12-12 is a string
email is a string
Cali is a string
Calle is a string
mypassword is a string
12334455es is a string
table 是使用此属性创建的
create table if not exists fakeclients (
id text,
corporate_account boolean,
name text,
last_name text,
cellphone text,
birth_day text,
email text,
password text,
city text,
address text,
primary key (id)
);
执行此查询的结果是
pq: syntax error at or near ","
但是如果我像这样在查询字符串中插入包含所有值的查询,它就可以工作
INSERT INTO fakeclients (uuid, name, last_name, birth_day, email, city, address, password, cellphone) VALUES ('f1571b24-d88e-42b3-907e-13f46efabacc is', 'myname', 'lastname', '2020-12-12', 'email', 'Cali', 'Calle', 'mypassword', '12334455es')
看起来 ExecContext 方法没有按预期解析参数
已解决,对于 postgres,插值查询的正确用法是使用 $1、$2、$3...
所以正确的查询应该是
INSERT INTO fakeclients (uuid, name, last_name, birth_day, email, city, address, password, cellphone) VALUES (, , , , , , , , )