1
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'成字符。

这里有什么问题?

4

1 回答 1

3

请注意,参数需要是一个序列:

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

逗号使它成为一个元组,而不是括号/括号。


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

sequence_of_sequences = [('11',), ('22',), ('33',)]
c.executemany("INSERT INTO dbtable (dbcolumn) VALUES (?)", sequence_of_sequences)
于 2013-04-02T16:36:00.217 回答