Do this instead<br><br>cur.execute('select * from Employee where id in (%s)', ids.join(', '));<br><br><div class="gmail_quote">On Wed, Dec 17, 2008 at 5:13 PM, Vijay Ramachandran <span dir="ltr"><<a href="mailto:vijay750@gmail.com">vijay750@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hello.<br><br>I'm unable to figure out how to use mysqldb (actually, dbabi) to write an "in" query.<br>
<br>For instance, suppose my table looks like this:<br>Employee(<br> id int not null primary key,<br> name varchar(32) not null)<br>
);<br><br>Suppose I have a list of ids, I would write sql such as<br>'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)'<br>
<br>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:<br><br><br>ids = [1, 2, 3]<br>if len(ids) > 1:<br> cur.execute('select * from Employee where id in %s', tuple(ids))<br>
else:<br> cur.execute('select * from Employee where id = %s', ids[0])<br><br>but it doesn't seem right. What's the correct way to run this query?<br><br>thanks,<br><font color="#888888">Vijay<br>
</font><br>_______________________________________________<br>
BangPypers mailing list<br>
<a href="mailto:BangPypers@python.org">BangPypers@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/bangpypers" target="_blank">http://mail.python.org/mailman/listinfo/bangpypers</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br>I am but a man.<br>