Re: data type mismatch: "a number is required, not str"
Adrian Klaver <[email protected]>
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <[email protected]> |
On Tuesday 26 January 2010 6:13:21 am Brian Jones wrote:
> Hi all,
>
> So, again, I'm trying to do a simple execute(), and I'm running into
> really unexpected errors. I have, of course, tried perhaps 5 different
> ways of putting the query together by now, and the error has not
> changed.
>
> Here's what I *currently* have in place in my code:
>
> sql = """INSERT INTO urls VALUES (%d, %d, %s, %s)"""
> params = [int(self.user_id), int(self.site_id), self.url_type
> , self.url]
> self.cursor.execute(sql,params)
>
> Of course, that's after trying simpler forms of the same thing.
> Namely, when I started this was all one line of code, and I didn't do
> explicit casts.
>
> self.cursor.execute("""INSERT INTO urls VALUES (%d, %d, %s, %s)""" %
> (self.user_id, self.site_id, self.url_type , self.url))
>
> The error I'm getting is:
> TypeError: %d format: a number is required, not str
>
> Explicit casting to int does not make this go away. Also, I tested by
> printing out "type(i) for i in params", and it prints out the types
> that I expect things to be, and the types I think I'm passing to the
> query, in the proper order.
>
> Does anyone see anything obvious that I might have goofed up?
> Thanks,
> brian
Yes, use only %s. From the docs(extensions.rst):
- The variables placeholder must always be a ``%s``, even if a different
placeholder (such as a ``%d`` for an integer) may look more appropriate
--
Adrian Klaver
[email protected]