On Jan 27, 2:55 am, Chris Nelson <chris.nel...@sixnet.com> wrote:
> On 01/26/2012 05:02 PM, osimons wrote:
> > On Jan 26, 10:25 pm, Chris Nelson<chris.nel...@sixnet.com>  wrote:
> >> On Jan 24, 5:15 pm, Chris Nelson<chris.nel...@sixnet.com>  wrote:
> >>> On 01/24/2012 05:07 PM, osimons wrote:
> > ...
> >> Is there a cleaner way?
>
> > Yes, manipulate the string to insert as many %s as there are items in
> > the users, then add users as args (or extend your existing args with
> > the users list to be replaced in same order). In Python:
>
> >   >>>  users = ["one", "two", "three"]
> >   >>>  sql = "SELECT * FROM table WHERE username in (%s)" %
> > ','.join(('%s',) * len(users))
> >   >>>  print sql
> >   'SELECT * FROM table WHERE username in (%s,%s,%s)'
> >   >>>  cursor.execute(sql, users)
>
> Great.  I got that one working.  Now I'm down to a an INSERT with
> integers *and* strings:
>
>              insert_cursor.execute("INSERT INTO %s " % self.table_name + \
>                                        "(ondate, username, availability) " \
>                                        "VALUES %s " % \
>                                        ", ".join(["('%s', '%s', %d)" %
>                                                   (t[0], t[1], t[2] and
> 1 or 0,)
>                                                   for t in inserts]))
>
> I don't think I can just put "%s %s %s" in there; I'm afraid that the DB
> API will try to store '1' (not 1).  Guess I'll try a couple of things....

Don't use %d - it is always %s regardless of underlying datatype.
Types may in theory even differ between backends, so just stick with
%s and it will be stored in correct type by the db api.

BTW, for a number of these questions I would recommend that you hooked
up with the #trac IRC channel at irc.reenode.net. Much quicker
turnaround for answers & understanding in a group chat:

http://trac.edgewall.org/wiki/IrcChannel

See you there!


:::simon

-- 
You received this message because you are subscribed to the Google Groups "Trac 
Development" group.
To post to this group, send email to trac-dev@googlegroups.com.
To unsubscribe from this group, send email to 
trac-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/trac-dev?hl=en.

Reply via email to