将python连接到sqlite3并在上填充多行

2024-06-28 20:21:24 发布

您现在位置:Python中文网/ 问答频道 /正文

这不是打印错误 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.

Tags: ofinabstractnumberfor错误entrezconn
1条回答
网友
1楼 · 发布于 2024-06-28 20:21:24

这不是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

相关问题 更多 >