我无法让Python的executemany让sqlite3正常工作

发布于 2024-10-23 22:55:46 字数 534 浏览 1 评论 0原文

我试图使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贪恋 2024-10-30 22:55:46

据我所知,你的意思是executemany,

clist = [("abc", ), ("def", ), ("ghi", )]
cursor.executemany("INSERT INTO myTable(data) values(?)", clist)

或者类似的东西。不要引用我关于 sqlite 语法的内容,我已经有一段时间没有在应用程序中使用它了,但是您需要一个可迭代的元组(更普遍的是可迭代的)。

看起来您收到的错误是它试图迭代您提供的每个字符串,因此您的语句的工作原理如下:

clist = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i')]

我不知道您的第二个查询试图完成什么,但它似乎解决了不同的问题表,所以我猜测没有架构信息,但是如果将单字符串更改为多字符字符串,它也会失败。

From what I know of executemany, you meant,

clist = [("abc", ), ("def", ), ("ghi", )]
cursor.executemany("INSERT INTO myTable(data) values(?)", clist)

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:

clist = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i')]

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.

够运 2024-10-30 22:55:46

只是为了补充上下文:在一种密切相关的情况下,我打算使用 executemany 将一个多元组列表插入到表中,如下所示:

res = [("John", "2j4o1h2n"), ("Paula", "lsohvoeemsy"), ("Ben", "l8ers")]

cur.executemany("INSERT INTO users (user, password) VALUES (?)", res)

期望 SQLite 一次获取一个元组(因此 < em>single ? VALUES 字段中的参数替换)并将其拆分为其封装的属性(在此为 ,情况),它失败并出现 sqlite3.ProgrammingError 异常 当前语句使用 1,并且提供了 2 个。 同样,因为 SQLite 期望在 中单独替换属性>VALUES (...) 字段。所以这解决了这个问题:

cur.executemany("INSERT INTO users (user, password) VALUES (?, ?)", res)

这是一个微不足道的案例,但可能有点混乱,我希望它可以帮助任何陷入困境的人。

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:

res = [("John", "2j4o1h2n"), ("Paula", "lsohvoeemsy"), ("Ben", "l8ers")]

cur.executemany("INSERT INTO users (user, password) VALUES (?)", res)

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 a sqlite3.ProgrammingError exception The current statement uses 1, and there are 2 supplied. as well, as SQLite expects separately substituted attributes in the VALUES (...) field. So this fixes it:

cur.executemany("INSERT INTO users (user, password) VALUES (?, ?)", res)

This is a trivial case but might confuse a little, I hope it might help whoever's stuck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文