我无法让Python的executemany让sqlite3正常工作
我试图使用executemany 将值插入数据库,但它对我不起作用。这是一个示例:
clist = []
clist.append("abc")
clist.append("def")
clist.append("ghi")
cursor.executemany("INSERT INTO myTable(data) values (?) ", clist)
这给了我以下错误:
sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用 1,并且提供了 3 个。
但是,当我更改列表时,它工作正常:
clist = ["a", "b"]
cursor.executemany("INSERT INTO myTable(data) values (?) ", clist)
它按预期工作!我可以看到数据库中的数据。为什么第一个列表不起作用而第二个列表起作用?
(PS:这只是一个示例,而不是实际的代码。为了简单起见,我做了一个小测试用例)。
I was trying to use executemany to insert values into a database, but it just won't work for me. Here is a sample:
clist = []
clist.append("abc")
clist.append("def")
clist.append("ghi")
cursor.executemany("INSERT INTO myTable(data) values (?) ", clist)
This gives me the following error:
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.
However, when I change the list, it works fine:
clist = ["a", "b"]
cursor.executemany("INSERT INTO myTable(data) values (?) ", clist)
It works as expected! I can see the data in the database. Why does the first list not work and second one does ?
(PS: This is just a sample and not the actual code. I made a small test case for simplicity).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,你的意思是executemany,
或者类似的东西。不要引用我关于 sqlite 语法的内容,我已经有一段时间没有在应用程序中使用它了,但是您需要一个可迭代的元组(更普遍的是可迭代的)。
看起来您收到的错误是它试图迭代您提供的每个字符串,因此您的语句的工作原理如下:
我不知道您的第二个查询试图完成什么,但它似乎解决了不同的问题表,所以我猜测没有架构信息,但是如果将单字符串更改为多字符字符串,它也会失败。
From what I know of executemany, you meant,
Or something similar. Don't quote me on the syntax for sqlite, I haven't used it in an app in a while, but you need an iterable of tuples (more generally iterables).
It looks like the error you're getting is that it's trying to iterate through each string you're providing, so your statement works like:
I don't know what your second query is trying to accomplish, but it appears to address a different table, so I'm guessing off of no schema info, but if you change the single character strings to multicharacter strings, it will fail too.
只是为了补充上下文:在一种密切相关的情况下,我打算使用
executemany
将一个多元组列表插入到表中,如下所示:期望 SQLite 一次获取一个元组(因此 < em>single
?
VALUES 字段中的参数替换)并将其拆分为其封装的属性(在此为,
情况),它失败并出现sqlite3.ProgrammingError
异常当前语句使用 1,并且提供了 2 个。
同样,因为 SQLite 期望在中单独替换属性>VALUES (...)
字段。所以这解决了这个问题:这是一个微不足道的案例,但可能有点混乱,我希望它可以帮助任何陷入困境的人。
Just to complement the context: In a closely related situation I meant to insert a list of poly-tuples into a table using
executemany
as such:Expecting SQLite to to take one tuple at a time (hence the single
?
parameter-substitution in the VALUES field) and splitting it up into its encapsulated attributes (<username>, <password>
in this case), it failed with asqlite3.ProgrammingError
exceptionThe current statement uses 1, and there are 2 supplied.
as well, as SQLite expects separately substituted attributes in theVALUES (...)
field. So this fixes it:This is a trivial case but might confuse a little, I hope it might help whoever's stuck.