[BangPypers] help using mysqldb

Vijay Ramachandran vijay750 at gmail.com
Wed Dec 17 12:43:06 CET 2008


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20081217/1df7cacb/attachment.htm>


More information about the BangPypers mailing list