Re: Problem using dynamic table names with pyformat
Gerhard Häring <[email protected]>
| Newsgroups | gmane.comp.python.db.pypgsql.user |
|---|---|
| Organization | OPUS GmbH |
| Message-ID | <[email protected]> |
Ganesan R <[email protected]> wrote: > On Thu, Feb 13, 2003 at 11:02:36PM -0800, Bob Terek wrote: >> >> > I then tried >> > >> > cursor.execute("SELECT * FROM %s WHERE id = %s" % table, 25) >> > >> > This doesn't work either because the % operator complains about "not enough >> > arguments for format string". I finally worked around by using >> > >> > Is there a syntactically clear alternative? Well, first, SELECT * sucks ;-) A combination of Python expansion and DB-API quoting should do. cursor.execute("SEELCT a, b, c FROM %s WHERE ID=%%s" % table, (25,)) Note the doubled percent sign to make the Python expansion leave the second %s (for the DB-API quoting) alone. >> The % operator expects a tuple, so this should work: >> >> cursor.execute("SELECT * FROM %s WHERE id = %s" % (table, 25)) >> >> but I haven't tried it of course. > > That works. But that will disable cursor optimization. [...] In theory, that's true. In reality, PostgreSQL started supporst PREPARE/EXECUTE only in 7.3, and we don't automatically use that. But you can *explicitely* use PREPARE/EXECUTE with pyPgSQL just fine. -- Gerhard ------------------------------------------------------- This SF.NET email is sponsored by: FREE SSL Guide from Thawte are you planning your Web Server Security? Click here to get a FREE Thawte SSL guide and find the answers to all your SSL security issues. http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0026en