SQL problem in python

Carsten Haese carsten at uniqsys.com
Sat Mar 8 12:18:00 EST 2008


On Sat, 2008-03-08 at 08:57 -0800, aiwarrior wrote:
>     def get_value( self, column ):
>         self.cursor.execute( "SELECT (?) FROM database", column )
>         for n in self.cursor:
>              print n

> 
> When i run it the get_value() returns 'filepath' instead of the
> columns. But if i dont use any variable and make the expression static
> all goes on as its supposed to. What am i doing wrong?

Using parameter binding wherever possible is a Good Idea, but parameter
binding can only be used to provide values. It can not be used to
provide syntax elements, table names, or column names. Basically, only
pieces that don't influence the query plan can be provided by
parameters.

To build a select query with a variable column name, you'll have to
resort to some kind of string building mechanism:

def get_value( self, column ):
    self.cursor.execute( "SELECT %s FROM database" % column )
    for n in self.cursor:
         print n

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list