带有绑定的Python sqlite查询按字符拆分

时间:2013-04-02 16:34:22

标签: python sqlite binding

some_list = ['11', '22', '33']
for i in some_list:
    c.execute("INSERT INTO dbtable (dbcolumn) VALUES (?)", (i))

返回错误:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied.

它会将'11'分解为字符。

这里有什么问题?

1 个答案:

答案 0 :(得分:3)

请注意,参数必须是一个序列:

    c.execute("INSERT INTO dbtable (dbcolumn) VALUES (?)", (i,))

逗号使其成为元组,而非parens / bracket。


或者,如果您可以将每个列表元素设为元组,则可以使用.executemany()方法:

sequence_of_sequences = [('11',), ('22',), ('33',)]
c.executemany("INSERT INTO dbtable (dbcolumn) VALUES (?)", sequence_of_sequences)