PostgreSQL, Python and BLOBs

Cliff Crawford cjc26 at nospam.cornell.edu
Sat Jun 16 20:07:49 EDT 2001


* Hans-Jürgen Schönig <hs at cybertec.at> menulis:
| Thank you ...
| Do you know an easier example?
| I don't know how to insert BLOBs yet. It works for me with C but I think
| some information is missing in the Python docs.

In PostgreSQL you can only access large objects within a transaction,
i.e. you need to do something like this:

import pg

db = pg.connect(dbname="...")
db.query("BEGIN;")
lo = db.locreate(pg.INV_READ|pg.INV_WRITE)
lo.open(pg.INV_WRITE)
lo.write(binary_data)
lo.close()
db.query("INSERT into blob_table VALUES (%d);" % lo.oid)
db.query("COMMIT;")

Note the two lines 'db.query("BEGIN;")' and 'db.query("COMMIT;")' -- if
you leave those out then the call to lo.open() will fail.


-- 
Cliff Crawford                   http://www.sowrong.org/
"Cliff, you're a god.  I want to bear your child." -- Ed



More information about the Python-list mailing list