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

怎么了

最佳答案

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

    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)

关于python - 带绑定(bind)的Python sqlite查询按字符将其拆分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15769440/

10-11 00:17