简体   繁体   English

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

[英]connect python to sqlite3 and fill multiple row in once

this is not printing Error the value of r=[bounch of number] and dont know how many and value of result is the name of by r 这不是打印错误r=[bounch of number]的值,并且不知道结果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()

it give Error: 它给出错误:

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.

This is not the correct usage of executemany . 这不是executemany的正确用法。 You can think of it acting like a nested for loop, where it iterates an outer container (representing a query) and then iterates the inner container which represents the data to be unpacked into the query. 您可以认为它的作用类似于嵌套的for循环,在此过程中,它迭代一个外部容器(表示一个查询),然后迭代一个内部容器,该容器表示要解压缩到查询中的数据。

However, in your case, you only have a single list, which presumably contains strings. 但是,根据您的情况,您只有一个列表,该列表可能包含字符串。 Thus, the inner "for" loop starts unpacking the characters of the string: 因此,内部的“ for”循环开始解压缩字符串的字符:

data = ['hello', 'something']

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

This is the actual use-case for executemany , where you want to unpack the values inside the inner container: 这是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

Just use execute . 只需使用execute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM