Python 错误:需要 int 参数
我在这里做错了什么?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两件事情。 首先,使用
%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 afteri
).Also,
ORDER
is a keyword, and should be escaped if you're using it as field name.我相信execute() 的第二个参数应该是一个可迭代的。 如果是这种情况,您需要将:更改
为:
以使其成为元组。
I believe the second argument to execute() is expected to be an iterable. IF this is the case you need to change:
to:
to make it into a tuple.
您可能应该使用
?
而不是%i
。 而且你还少了一个括号。You should be using
?
instead of%i
probably. And you're missing a parenthesis.