加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – psycopg2 executemany简单列表?

发布时间:2020-12-20 11:30:19 所属栏目:Python 来源:网络整理
导读:我正在尝试使用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 (c
我正在尝试使用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 ])

如果不使用“named”参数(%(value)s),是不是可以给出一个“简单”的列表/元组值?

解决方法

executemany需要一系列序列,例如.列表清单:

[[v] for v in values]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读