correct syntax for db api..

Gerhard Häring gerhard.haering at gmx.de
Mon Jul 22 14:04:52 EDT 2002


* Stefan Schwarzer <sschwarzer at sschwarzer.net> [2002-07-22 19:41 +0200]:
> Hi Eugene Kim
> 
> eugene kim wrote:
> >import pgdb
> >
> >db=pgdb.connect(user='postgres',database='test')
> >cursor=db.cursor()
> >
> >-----------------
> >cursor.execute('select url, visits from history where url= %s', 
> >(self.url))     -------------
> 
> As a side note, (self.url) is an expression, not a tuple. Both should work.

Only the tuple form is guaranteed to work with the DB-API. Though most
modules implement the nonstandard extension of a single argument as the
value paramater. You can make a tuple of length one like so: (1,)

> >or
> >-----------------
> >sqlselect = 'select url, visits from history where url = %s '
> >cursor.execute(sqlselect % self.url)
> >----------------
> 
> Perhaps you have to use quotes:
> 
> sqlselect = "select url, visits from history where url='%s'"
> cursor.execute(sqlselect % self.url)

No! Definitely not.

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))





More information about the Python-list mailing list