Python 错误:需要 int 参数

发布于 2024-07-29 05:50:13 字数 810 浏览 7 评论 0原文

我在这里做错了什么?

 i = 0
 cursor.execute("insert into core_room (order) values (%i)", (int(i))

错误:

 int argument required

数据库字段是 int(11),但我认为 %i 正在生成错误。

更新:

这是一个更彻底的示例:

time = datetime.datetime.now()
floor = 0
i = 0

尝试: booster_cursor.execute('插入 core_room (extern_id, name, order, unit_id, 创建, 更新) 值 (%s, %s, %s, %s, %s, %s)', (row[0], row [0],i,楼层,时间,时间,)) 除了异常,e: 打印 e

错误:

  (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order, unit_id, created, updated) values ('99', '99', '235', '12', '2009-07-24 1' at line 1")

What am I doing wrong here?

 i = 0
 cursor.execute("insert into core_room (order) values (%i)", (int(i))

Error:

 int argument required

The database field is an int(11), but I think the %i is generating the error.

Update:

Here's a more thorough example:

time = datetime.datetime.now()
floor = 0
i = 0

try:
booster_cursor.execute('insert into core_room (extern_id, name, order, unit_id, created, updated) values (%s, %s, %s, %s, %s, %s)', (row[0], row[0], i, floor, time, time,))
except Exception, e:
print e

Error:

  (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order, unit_id, created, updated) values ('99', '99', '235', '12', '2009-07-24 1' at line 1")

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

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

发布评论

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

评论(3

池予 2024-08-05 05:50:13

两件事情。 首先,使用 %s 而不是 %i。 其次,参数必须位于元组中 - 因此您需要 (i,) (在 i 后面加上逗号)。

此外,ORDER 是一个关键字,如果您将其用作字段名称,则应将其转义。

Two things. First, use %s and not %i. Second, parameters must be in a tuple - so you need (i,) (with comma after i).

Also, ORDER is a keyword, and should be escaped if you're using it as field name.

寄意 2024-08-05 05:50:13

我相信execute() 的第二个参数应该是一个可迭代的。 如果是这种情况,您需要将:更改

(int(i))

为:

(int(i),)

以使其成为元组。

I believe the second argument to execute() is expected to be an iterable. IF this is the case you need to change:

(int(i))

to:

(int(i),)

to make it into a tuple.

清醇 2024-08-05 05:50:13

您可能应该使用 ? 而不是 %i 。 而且你还少了一个括号。

cursor.execute("insert into core_room (order) values (?)", (int(i),))

You should be using ? instead of %i probably. And you're missing a parenthesis.

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