<div dir="ltr">       cursor.execute("""<br>                   SELECT titem.object_id, titem.tag_id<br>                   FROM tagging_taggeditem titem<br>                   WHERE titem.object_id IN (%s)<br>
               """ % ','.join([str(x) for x in [1,5,9]])<br><br><div class="gmail_quote">On Fri, Sep 26, 2008 at 6:23 AM, Tino Wildenhain <span dir="ltr"><<a href="mailto:tino@wildenhain.de">tino@wildenhain.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi,<div class="Ih2E3d"><br>
<br>
Bruno Desthuilliers wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
bcurtu a écrit :<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I have a BIIIIIG problem with the next query:<br>
<br>
        cursor.execute("""<br>
                    SELECT titem.object_id, titem.tag_id<br>
                    FROM tagging_taggeditem titem<br>
                    WHERE titem.object_id IN (%s)<br>
                """,( eid_list))<br>
<br>
eid_list is suppossed to be a list of ids = [1,5,9]<br>
<br>
How can I make it work?<br>
</blockquote>
<br>
You have to build your sql statement in three stages:<br>
<br>
# stage 0: the template<br>
sql_template = """<br>
    SELECT titem.object_id, titem.tag_id<br>
    FROM tagging_taggeditem titem<br>
    WHERE titem.object_id IN (%s)<br>
"""<br>
<br>
# stage 1: build correct place_holders string for the actual number<br>
# of items in eid_list<br>
place_holders = ", " .join("%s" for x in xrange(len(eid_list)))<br>
</blockquote>
<br></div>
Hm. either ", ".join(["%s"]*len(eid_list))<br>
or ", ".join("%s" for x in eid_list)<br>
<br>
should produce the same, wouldn't it? :-)<div class="Ih2E3d"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
# stage 2 : build the effective sql statement<br>
sql = sql_template % place_holders<br>
<br>
# ok, let's go:<br>
cursor.execute(sql_template, eid_list)<br>
<br>
<br>
NB : you can of course make it in a single statement, but readability will suffer:<br>
<br>
cursor.execute(<br>
    """<br>
    SELECT titem.object_id, titem.tag_id<br>
    FROM tagging_taggeditem titem<br>
    WHERE titem.object_id IN (%s)<br>
    """ % ", " .join("%s" for x in xrange(len(eid_list))),<br>
    eid_list<br>
    )<br>
</blockquote>
<br></div>
I'd think giving the arguments in a form of an array type should<br>
work too. At least in postgres there are references to do so.<br>
<br>
Regards<br><font color="#888888">
Tino<br>
<br>
</font><br>--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br><br clear="all"><br>-- <br>| _ | * | _ |<br>| _ | _ | * |<br>
| *  | * | * |<br>
</div>