将python连接到sqlite3并一次填充多行

时间:2018-10-10 08:29:22

标签: python sqlite

这不是打印错误 r=[bounch of number]的值,并且不知道结果的值是r的名称

conn = sqlite3.connect('/home/cbbi-l2-16/Desktop/karim')
c = conn.cursor()

print ("Opened database successfully")
example = [r,result]

for row in c.executemany("INSERT INTO Entrez (PuId,Abstract) VALUES 
(?,?)",(r,resul)):
    print (row)

conn.commit()
c.close()

给出错误:

Traceback (most recent call last):
  File "sqlpython.py", line 60, in <module>
    for row in c.executemany("INSERT INTO Entrez (PuId,Abstract) VALUES (?,?)",(r,resul)):
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 2, and there are 5 supplied.

1 个答案:

答案 0 :(得分:0)

这不是executemany的正确用法。您可以将其视为嵌套的for循环,在其中循环访问外部容器(表示查询),然后循环访问内部容器(表示要解压缩到查询中的数据)。

但是,在您的情况下,您只有一个列表,该列表可能包含字符串。因此,内部的“ for”循环开始解压缩字符串的字符:

data = ['hello', 'something']

for item in data:
    for subitem in item:
        print(subitem) # this is what it's trying to insert

这是executemany的实际用例,您想在其中拆开内部容器中的值:

data = [['hello', 'something'], ['goodbye', 'something_else']]
for item in data:
    for subitem in item:
        print(subitem) # this is what it's trying to insert

只需使用execute