[Tutor] Dynamic inserts into postgresql

Lloyd Kvam pythontutor at venix.com
Sat Sep 6 11:02:58 EDT 2003


http://www.python.org/peps/pep-0249.html
Normally this would give the details for using the DBI.  However, the
content is missing (<html><body></body></html>) as I write this.

I'll try to fill in from memory (I have not used pg).
The module should have an attribute paramstyle that describes how it
handles variable substitution into queries.

MySQLdb as a paramstyle of format.  It works essentially as follows:
import MySQLdb as SQL
 >>> SQL.paramstyle
'format'

conn = SQL.connect(...)
curs = conn.cursor()
...
curs.execute("INSERT INTO tablename (field1,field2) VALUES (%s,%s)",(a,b))

The values from the a and b variables are interpolated into the %s place
holders.  Quoting and format issues are handled by the MySQLdb module.

Determine which paramstyle pg uses.  Find the PEP249 which documents the
DBI 2 standard and explains how to use each paramstyle.


Peter Brown wrote:

> Dear Tutor,
> 
> I feel like a dummy for asking this, but I have been working on it for 2
> days without resolution.
> 
> I'm using pygresql module pg (not pgdb) to communicate to PostgreSQL I can
> connect, open cursor, insert static data, select etc.
> 
> But what I can't do is insert data that is not fixed ie using variables. eg
> statement = " INSERT INTO target VALUES (a, b)" ; res = db.query(statement)
> 
> Any help would be greatly accepted
> 
> P
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582




More information about the Tutor mailing list