Newbie help (TypeError: int argument required)

shubalubdub at gmail.com shubalubdub at gmail.com
Sun Jun 8 15:56:45 EDT 2008


On Jun 8, 1:43 pm, Iain Adams <aca04... at shef.ac.uk> wrote:
> Hi,
>
> I am new to python. I have been having trouble using the MysqlDB. I
> get an error pointing from the line
>
> cursor.execute("UPDATE article SET title = %s, text = %s WHERE id =
> %u", (self.title, self.text, self.id))
>
> Here is the error:
>
>  line 56, in save
>     cursor.execute("UPDATE article SET title = %s, text = %s WHERE id
> = %u", (self.title, self.text, self.id))
>   File "/var/lib/python-support/python2.5/MySQLdb/cursors.py", line
> 151, in execute
>     query = query % db.literal(args)
> TypeError: int argument required
>
> However when I print out type(self.id) I get <type 'int'>.
>
> So surely I have provided an int argument.
>
> Any ideas anyone??
From MySQLdb User's Guide (http://mysql-python.sourceforge.net/
MySQLdb.html):

To perform a query, you first need a cursor, and then you can execute
queries on it:

  c=db.cursor()
  max_price=5
  c.execute("""SELECT spam, eggs, sausage FROM breakfast
            WHERE price < %s""", (max_price,))

In this example, max_price=5 Why, then, use %s in the string? Because
MySQLdb will convert it to a SQL literal value, which is the string
'5'. When it's finished, the query will actually say, "...WHERE price
< 5".



More information about the Python-list mailing list