python - 将python连接到sqlite3并一次填充多行
问题描述
这不是打印错误的值r=[bounch of number]
并且不知道结果的数量和值是 by 的名称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.
解决方案
这不是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
.
推荐阅读
- swift - 如何在 SwiftUI 中使用 UserData 可观察对象?
- ios - iPhone 11 Pro Max 上带有故障内容的 UIScrollView
- parameters - react.js 将多个参数传递给箭头函数
- c++ - 在 OpenGL 中使用 C++ 纹理化 3d 多边形
- javascript - 有没有办法在 Chrome 开发者工具中显示所有定义的类名?
- java - 如何使用 Java 进行拓扑排序(依赖解析)
- reactjs - Firebase:无法发送 OTP
- python - 括号内的for循环是什么
- r - 如何悄悄地更改 R 中的系统区域设置
- opencart - 如何更改打开购物车中的关键字?