[DB-SIG] PyGresSql help
Tom Bryan
tbryan@server.python.net
Tue, 25 Jan 2000 07:51:58 -0500 (EST)
On Mon, 24 Jan 2000, Jim Johannsen wrote:
> I am trying to use PyGresSQL to connect Python and Postgress. I have
> managed to connect to the db, but cannot update the database with a list
> or with strings. I have tried the following code w/o success:
>
> cnx.query("INSERT INTO county VALUES(%s,%s,%s)") %(Ldata[0], Ldata[1],
> Ldata[3])
...
> and countless variations of the above. Every variation but the correct
> one.
I think that this is more of a generic Python syntax problem than a
database problem. I would guess that the string interpolation must
occur inside of the function argument. For example,
Python 1.5.2 (#1, Apr 18 1999, 16:03:16) [GCC pgcc-2.91.60 19981201
(egcs-1.1.1 on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> def foo(text):
... print text
...
>>> foo("%s, %s, %s") % ('one','two','three')
%s, %s, %s
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: bad operand type(s) for %
>>> foo( "%s, %s, %s" % ('one','two','three') )
one, two, three
>>>
So, I'd suggest the following. (Note that the whitespace isn't really
necessary: I'm just trying to emphasize how the parentheses nest.)
cnx.query (
"INSERT INTO county VALUES(%s,%s,%s)" % (
Ldata[0], Ldata[1],Ldata[3]
)
)
---Tom