使用 iteritems() 将字典插入到 sqlite3 数据库中

发布于 2024-10-29 23:11:45 字数 626 浏览 0 评论 0原文

我的“video_episodes”表就像:

CREATE TABLE "video_episodes" (
    "id" integer NOT NULL PRIMARY KEY,
    "dizilink_id" integer NOT NULL REFERENCES "video_dizilink" ("id"),
    "episodename" varchar(50) NOT NULL,
    "episodeurl" varchar(200) NOT NULL);

我试图在字典中插入一些值:

video_links = {'a': '1', 'b': '2'}
cursor.executemany("""INSERT INTO video_episodes(dizilink_id, episodename, episodeurl) VALUES (?,?,?)""", dizilink_id, video_links.iteritems(),)
TypeError: function takes exactly 2 arguments (3 given)

如果没有 dizilink_id,我可以使用 iteritems() 插入,但我无法理解如何使用附加键来插入。

My "video_episodes" table is like:

CREATE TABLE "video_episodes" (
    "id" integer NOT NULL PRIMARY KEY,
    "dizilink_id" integer NOT NULL REFERENCES "video_dizilink" ("id"),
    "episodename" varchar(50) NOT NULL,
    "episodeurl" varchar(200) NOT NULL);

I'm trying to insert some values within a dict:

video_links = {'a': '1', 'b': '2'}
cursor.executemany("""INSERT INTO video_episodes(dizilink_id, episodename, episodeurl) VALUES (?,?,?)""", dizilink_id, video_links.iteritems(),)
TypeError: function takes exactly 2 arguments (3 given)

Without dizilink_id, I'm able to insert with iteritems() but I can't understand how it's possible with additional key(s).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

-残月青衣踏尘吟 2024-11-05 23:11:45

这与 iteritems 无关。

正如错误消息所示,executemany(为什么使用 Many,而不是简单的 execute?)需要两个参数,而您传递了三个参数。 SQL 语句的参数是单个参数,因此需要包装在元组中。

cursor.execute("""INSERT INTO video_episodes(dizilink_id, episodename, episodeurl) VALUES (?,?,?)""", (dizilink_id, video_links.iteritems()))

评论后编辑 好的,我想我明白你现在想要做什么。您想要为 dizilink_id 的相同值插入多个(episodename、episodeurl)值。

你不能用executemany 做到这一点。您需要运行多个命令。像这样的东西:

for name, url in video_links.iteritems():
    cursor.execute("INSERT INTO video_episodes(dizilink_id, episodename, episodeurl) "
                   " VALUES (?,?,?)", (dizilink_id, name, url))

This has nothing to do with iteritems.

As the error message says, executemany (why are you using many, rather than simple execute?) takes two arguments, and you are passing three. The parameters to the SQL statement are a single parameter, so need to be wrapped in a tuple.

cursor.execute("""INSERT INTO video_episodes(dizilink_id, episodename, episodeurl) VALUES (?,?,?)""", (dizilink_id, video_links.iteritems()))

Edit after comment OK, I think I understand what you are trying to do now. You want to insert multiple values of (episodename, episodeurl) for the same value of dizilink_id.

You can't do that with executemany. You'll need to run multiple commands. Something like this:

for name, url in video_links.iteritems():
    cursor.execute("INSERT INTO video_episodes(dizilink_id, episodename, episodeurl) "
                   " VALUES (?,?,?)", (dizilink_id, name, url))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文