python - 如何将 postgres 数组的值作为参数传递给 asyncpg connection.execute?
问题描述
我正在尝试构建一段代码,用于asyncpg
将内容添加到我的 postgres 数据库中的表中,定义如下:
CREATE TABLE my_table (
id SERIAL NOT NULL UNIQUE,
nested_field varchar(100) NOT NULL UNIQUE,
subfields varchar(100)[]
);
从我的 POV 来看,困难的部分是将内容保存到postgres 数组变量中。
我构建的代码如下:
try:
await connection.execute(query, thing_string, subfields_string)
return None
except (Exception, asyncpg.UniqueViolationError) as integrError:
# some other action
except (Exception, asyncpg.ConnectionFailureError) as error:
# some other action
finally:
# some other action
它运行的地方query
定义为:
query = """
INSERT INTO my_table(thing, subfields)
VALUES($1,$2);
"""
和args*
(这里是关于asyncpgargs*
函数参数的文档)connection.execute
将作为 $1 和 $2 放入字符串中
thing_string
, 定义为thing_string = "something"
和
subfields_string
,通过运行该行获得
subfields_string = from_list_to_stringified_set(list_of_subfields)
在哪里
list_of_subfields = ["one, two, three"]
函数定义如下:
def from_list_to_stringified_set(list_of_subfields):
"""
Given a list of subfields
[ "subfield1", "subfield2", "subfield3" ]
it returns
'{ "subfield1", "subfield2", "subfield3" }'
"""
subfields_string = ""
for subfield in list_of_subfields:
subfields_string = subfields_string + '", "' + subfield
subfields_string = '{' + subfields_string[3:] + '"}'
return subfields_string
这样subfields_string
结果的值'{"one, two, three"}'
(我的代码正确地实现了这个结果)。
为了正常工作,在数据库上运行的查询应该是:
# desired result INSERT INTO my_table(title, subfields) VALUES('something','{"one", "two", "three"}');
但是,当我尝试运行我得到的脚本时
asyncpg.exceptions.DataError: invalid input for query argument $2: '{"one", "two", "three"}' (a sized iterable container expected (got type 'str'))
所以connection.execute(...)
不接受我的第二个参数 ,subfields_string
具有 value '{"one, two, three"}'
,因为显然它想要一个可迭代的而不是字符串。
但为什么?
我connection.execute(...)
作为其中一部分传递的其他参数args*
也是字符串,那么为什么第二个参数被拒绝而第一个被接受呢?
以及如何更改我的代码以获得# desired result
?
解决方案
使用字符串列表作为参数。
query = """
INSERT INTO my_table(nested_field, subfields)
VALUES($1,$2);
"""
thing_string = 'something'
subfields_string = ["one", "two", "three"]
await connection.execute(query, thing_string, subfields_string)
该列subfields
是一个 varchar 数组。它对应的 Python 类型是一个列表,如文档中所述
推荐阅读
- docker - kafka_connect 没有找到它的连接器
- python - 即使不应该有语法错误 (.configure()) 的语法错误
- c# - 在 AzureDataFactory 管道中创建 LinkedServe 时,自托管集成运行时不可用
- angular - 如何在 Firestore Angular7 中使用 orderBy 和 snapshotChanges()
- javascript - ngRequired 未在表单字段上设置 $error 属性
- xml - sed 在提到开始和结束模式时删除多行
- android - 检测何时达到 TVertScrollBox 底部然后执行一些程序
- apache-kafka - Kafka Streams:在标点函数中生成的消息的 Exactly-once 语义
- c++ - findChessboardCorners() 无法检测背光条件下的角点
- python - 为什么while循环只循环if语句的一部分?