slypete
slypete

Reputation: 5648

Python error: int argument required

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")

Upvotes: 0

Views: 5621

Answers (3)

Pavel Minaev
Pavel Minaev

Reputation: 101565

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.

Upvotes: 4

Evan Fosmark
Evan Fosmark

Reputation: 101681

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

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

Upvotes: 1

Mark Roddy
Mark Roddy

Reputation: 27936

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.

Upvotes: 1

Related Questions