[BangPypers] help using mysqldb

Sidharth Kuruvila sidharth.kuruvila at gmail.com
Wed Dec 17 12:59:27 CET 2008


Do this instead

cur.execute('select * from Employee where id in (%s)', ids.join(', '));

On Wed, Dec 17, 2008 at 5:13 PM, Vijay Ramachandran <vijay750 at gmail.com>wrote:

> Hello.
>
> I'm unable to figure out how to use mysqldb (actually, dbabi) to write an
> "in" query.
>
> For instance, suppose my table looks like this:
> Employee(
>  id int not null primary key,
>  name varchar(32) not null)
> );
>
> Suppose I have a list of ids, I would write sql such as
> 'select * from Employee where id in (id1, id2, id3)' and this would work
> even if there was only one id, say, 'select * from Employee where id in
> (id1)'
>
> How should I do this in MySQLdb? It seems that I need to handle single
> entry list differently from a list with multiple elements? i.e., this code
> below works:
>
>
> ids = [1, 2, 3]
> if len(ids) > 1:
>     cur.execute('select * from Employee where id in %s', tuple(ids))
> else:
>     cur.execute('select * from Employee where id = %s', ids[0])
>
> but it doesn't seem right. What's the correct way to run this query?
>
> thanks,
> Vijay
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>


-- 
I am but a man.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20081217/5d07e7b2/attachment.htm>


More information about the BangPypers mailing list