gpt4 book ai didi

python - psycopg2 executemany 与简单列表?

转载 作者:太空宇宙 更新时间:2023-11-03 13:01:33 29 4
gpt4 key购买 nike

我正在尝试将 psycopg2 executemany 用于简单的多插入,但我只能使用 dict 而不是“普通”值序列使其工作:

# given:
values = [1, 2, 3] ; cursor = conn.cursor()

# this raises TypeError: 'int' object does not support indexing:
cursor.executemany('INSERT INTO t (col_a) VALUES ( %s )', values)
# I also tried encapsulating my 'values' into a tuple/list but it gives another exception (TypeError: not all arguments converted during string formatting).

# while this is ok:
cursor.executemany('INSERT INTO t (col_a) VALUES ( %(value)s )', [ dict(value=v) for v in values ])

是否可以在不使用“命名”参数 (%(value)s) 的情况下给出一个“简单”的值列表/元组?

最佳答案

executemany 需要一系列序列,例如。列表列表:

[[v] for v in values]

关于python - psycopg2 executemany 与简单列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19154324/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com