[Tutor] _pg module and the insertion of random numbers...
Kalle Svensson
kalle@gnupung.net
Wed, 11 Apr 2001 13:14:13 +0200
Sez kromag@nsacom.net:
> I am attempting to insert random numbers into a PostGreSQL database using _pg
> and random. I am unsure as to syntax when adding variables.
>
> I have attempted the following:
>
> import _pg
> import random
> db.connect('test', 'localhost')
> db.query("insert into pork values(`random.randint(1,1000)`+ 'stuff' +'1-2-
> 1999')")
db.query("insert into pork values(" +
`random.randint(1,1000)` +
", 'stuff', '1-2-1999')")
should work, but I haven't tested. Also note that use of backticks for
stringification is quite uncommon. Using the builtin functions str() or
repr() (which is equivalent to backticks) is far more common.
repr(123) == `123` # this is always true
str(123) == `123` # this isn't always true, but works for integers.
Another way to do it is to use the % operator:
"insert into pork values(%d, 'stuff', '1-2-1999')" % random.randint(1,1000))
> What am I doing wrong?
I think you got lost in a maze of twisty little quotes, all alike. <wink>
Peace,
Kalle
--
Email: kalle@gnupung.net | You can tune a filesystem, but you
Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
[ Not signed due to lossage. Blame Microsoft Outlook Express. ]