[DB-SIG] Newbie: PyGreSQL and Insertion of random numbers

Federico Di Gregorio fog@mixadlive.com
Wed, 11 Apr 2001 08:55:41 +0200


Scavenging the mail folder uncovered kromag@nsacom.net's letter:

> db.query("insert into pork values('`random.randint(1,1000)`', 'stuff', '1-2-
> 1999')")
> 
> which, of course inserts random.randint() as a literal string.
> 
> What am I doing wrong?

first, you are not using DB API calls while writing to the DB API SIG ;)
anyway, with *any* DB API compliant python module you can do as follows:

import random
import module # put your module name here, not "module"

o = module.connect("test", host="localhost")
# some modules support anly/also o = module.connect("dbname=test host=localhost")
c = o.cursor()
c.execute("INSERT INTO pork VALUES (%d, 'stuff', '1-2-1999')", random.randint(1,1000))

-- 
Federico Di Gregorio
MIXAD LIVE Chief of Research & Technology              fog@mixadlive.com
Debian GNU/Linux Developer & Italian Press Contact        fog@debian.org
  Qu'est ce que la folie? Juste un sentiment de liberté si
   fort qu'on en oublie ce qui nous rattache au monde... -- J. de Loctra