- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试将数据从 Python(3.9.1) 编译到 SQLite3 时出现错误。
import sqlite3
conn = sqlite3.connect('newdB.db')
#Creates the table 'tbl_newdb' with an incrementing ID, and 'db_type' columns.
with conn:
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS tbl_newdb( \
ID INTEGER PRIMARY KEY AUTOINCREMENT, \
db_type TEXT)")
conn.commit()
conn.close()
conn = sqlite3.connect('newdB.db')
#Our list of data we will be grabbing from to fill the tables.
with conn:
cur = conn.cursor()
fileList = "information.docx","Hello.txt","myImage.png","myMovie.mpg","World.txt","data.pdf","myPhoto.jpg"
cur.execute("INSERT INTO tbl_newdb (db_type) VALUES (?)",(fileList,))
conn.commit()
conn.close()
这是我从 IDLE shell 收到的错误消息:
line 22, in <module>
cur.execute("INSERT INTO tbl_newdb (db_type) VALUES (?)",(str(fileList,)))
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 103 supplied.
最佳答案
TLDR:execute
接受两个参数,一个 SQL 查询和一个输入列表,其中 SQL 中的每个 (?)
参数都有一个元素。
您想要的是 executemany
它接受输入列表的列表。解决方案如下所示:
import sqlite3
# Create a database in memory for testing
conn = sqlite3.connect('newdB.db')
# Execute with the databse connection
with conn:
cur = conn.cursor()
# Create your table in the in-memory database
cur.execute("""
CREATE TABLE IF NOT EXISTS tbl_newdb(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
db_type TEXT
)
""")
conn.commit()
# A list of parameters to pass to queries
# Notice we are making a list of lists
# `execute` takes a list of inputs for a single execution
parameterList = [
["information.docx"],
["Hello.txt"],
["myImage.png"],
["myMovie.mpg"],
["World.txt"],
["data.pdf"],
["myPhoto.jpg"],
]
with conn:
# Execute the same query multiple times
# Because we are passing one value to our sql, each list in parameterList
# only has one element.
cur.executemany(
"INSERT INTO tbl_newdb(db_type) VALUES(?)",
parameterList
)
conn.commit()
# Print debugging information to prove we inserted things correctly
cur.execute("SELECT db_type FROM tbl_newdb")
print(cur.fetchall())
# Close the connection after creating the table
conn.close()
注意我使用 [...]
语法而不是元组 (...,)
语法。两者都可以,为了清晰起见,我只是更喜欢方括号,因为我们有很多单元素列表。
您还可以用 for 循环替换 cur.exeutemany(...)
,如下所示:
一点解释:execute
接受两个参数,一个 SQL 查询和一个参数列表。您可以将参数列表视为 SQL 查询中每个 (?)
的替换。第一个 (?)
将替换为参数列表中的第一个元素,第二个 (?)
将替换为列表中的第二个元素,依此类推。
让我们弄清楚我们是如何到达这里的。当我第一次运行你的代码时,我得到了这个输出:
Traceback (most recent call last):
File "alt.py", line 20, in <module>
cur.execute("INSERT INTO tbl_newdb (db_type) VALUES (?)",(fileList,))
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
不支持的类型
向我暗示,您将已经是一个元组的fileList
放入其中,并将其包装在另一个元组中。所以我将 execute
行更改为:
cur.execute("INSERT INTO tbl_newdb (db_type) VALUES (?)", fileList)
接下来我们得到这个错误:
Traceback (most recent call last):
File "alt.py", line 20, in <module>
cur.execute("INSERT INTO tbl_newdb (db_type) VALUES (?)", fileList)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 7 supplied.
这很奇怪。它正在获取我列出的 7 件事并试图将它们塞进一个绑定(bind)中?有趣的。如果我们将 fileList
缩短为只有一个元素会怎样?从小事做起,看看我们能取得什么成果。我将 execute
上方的行更改为
fileList = "information.docx"
我们得到了一个类似但不同的错误:
Traceback (most recent call last):
File "alt.py", line 20, in <module>
cur.execute("INSERT INTO tbl_newdb (db_type) VALUES (?)", fileList)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 16 supplied.
嗯。 16 是 "information.docx"
中的字符数,因此 Python 必须将该字符串视为参数列表。让我们将其包装在一个列表中,看看会发生什么:
fileList = ["information.docx"]
这样我们就取得了第一次成功!
如果我们向列表中添加另一个元素会发生什么?
fileList = ["information.docx", "other_file.txt"]
Traceback (most recent call last):
File "alt.py", line 20, in <module>
cur.execute("INSERT INTO tbl_newdb (db_type) VALUES (?)", fileList)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied.
科学!它在一个元素上成功,但在两个元素上失败。这意味着 execute
的第二个参数需要是一个列表,其元素数量与 SQL 查询中的 (?)
参数一样多。
现在我们有几个选择。我们可以 read the SQLite Python docs and discover executemany
,或者我们可以做一个 for 循环:
for f in fileList:
parameters = [f]
cur.execute("INSERT INTO tbl_newdb (db_type) VALUES (?)", parameters)
conn.commit()
SQLite 非常快,因此 executemany
和 for 循环都会产生相似的性能。
关于python - sqlite3.InterfaceError : Error binding parameter 0 - probably unsupported type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66286198/
Apple M1 芯片上的 Mac OS 11.6 节点版本是17.0.1 % node -v v17.0.1 这个错误似乎真的来 self 无法辨别的任何地方。 (我检查了我的软件更新,没有最近的操
运行 cv2.getRectSubPix(img, (5,5), (0,0)) 抛出错误: OpenCV Error: Unsupported format or combination of for
不可能在 itunesconnect 中发送应用程序的新版本。虽然上周,同样的版本发送发生了。在代码中,我只是改了一个按钮的名字,没有再改哪里。 但总是报错: 错误 ITMS-9000:“不支持的架构
鉴于下面的 HTML,我尝试使用 jQuery 来匹配所有具有类“foo”的跨度的列表项,并且该跨度应包含文本“relevant”。 Some text relevant Some more
Azure 开始出现以下错误: Unsupported token. Unable to initialize the authorization context. 每当我尝试更改我的应用程序时,我都
尝试安装friday软件包时,出现错误 Preprocessing library friday-0.2.2.0... src/Vision/Detector/Edge.hs:3:14: Unsupp
Azure 开始出现以下错误: Unsupported token. Unable to initialize the authorization context. 每当我尝试更改我的应用程序时,我都
我的代码抛出此错误 Failed to load resource: unsupported url在以下行: self.$el.find('.capturedImage').attr('src',
首先,请原谅它已经被问过或者可以很容易地通过谷歌找到。我发布这个是因为我的时间有限。这是录制音频和视频的代码。 stopPreview(); Log.d("stream
我正在尝试在 Android 上实现应用索引。 我有一个 Intent 过滤器,如下:
对于在 WAS 8.5 中运行的应用程序客户端,我们有代码创建套接字失败并出现以下错误。 SSLSocketFactory factory = (SSLSocketFactory) SSLSo
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我正在尝试从序列中获取生成的 key 。(使用 Servlet 和 Oracle10) 以下是我的代码: query ="insert into TABLE_NAME(COL1,COL2,COL3)
我的项目中有这个功能: def clean(self): if self.id_document_type == 'BC': now = date.today()
我在尝试 ARKit 时出现黑屏并显示以下错误消息。 WWDC 2017 示例 PlacingObjects 在同一设备上运行没有问题。 [] >>> FigVirtualFramebufferGet
ffmpeg 缺乏对 AAC 的默认支持真的很烦人: 我的 ffmpeg 版本: ffmpeg version git-2020-05-02-0d81edc Copyright (c) 2000-20
以下 config.xml 导致错误: ... ... ... 错误信息: Error Image 代码库是继承的,据我所知
创建位图上下文时出现此错误: CGBitmapContextCreate:不支持的参数组合:8 个整数位/组件; 24 位/像素;三分量色彩空间; kCGImageAlphaNone; 7936 字节
使用 pandas 读取 .xlsx 文件时出错。看起来它正在打开文件,因为它能够读取列名的前 8 个字符,即 员工编号 但因此错误而失败。我看到很多关于这个的帖子,但最后一部分从来都不是这些错误
我尝试执行下面的代码,但它抛出以下错误消息: Error: Syntax error, unrecognized expression: unsupported pseudo: really-good
我是一名优秀的程序员,十分优秀!