[Tutor] loading an image into a Postgre database

Mike Hansen mhansen at cso.atmel.com
Tue Jun 21 22:43:28 CEST 2005


I'm having trouble loading an image into a Postgre database. The code is below 
as well as the traceback in the apache log. Is something up with my 
sqlStatement? Do I need to use something other than %s? How can I avoid that 
type error?

Thanks,

Mike


#! /usr/bin/env python

import cgi
from pyPgSQL import PgSQL
# from pyPgSQL.PgSQL import PgBytea

def main():
     form = cgi.FieldStorage()
     if form.has_key("filename"):
         item = form["filename"]
         imageName = form["imagename"]
         if item.file:
             data = item.file.read()
             data_obj = PgSQL.PgBytea(data)
             connectdb =PgSQL.connect('server:port:database:username:password')
             cur = connectdb.cursor()
             sqlStatement = """INSERT INTO images (image)
                               VALUES (%s);
                            """ % (data_obj)
             cur.execute(sqlStatement)
             cur.close()
             connectdb.commit()
             print "Content-type: text/html\n\n"
             print "image loaded"


if __name__ == '__main__':
     main()


Traceback (most recent call last):
   File "/var/www/htdocs/mtest/imagetest.py", line 28, in ?
     main()
   File "/var/www/htdocs/mtest/imagetest.py", line 20, in main
     cur.execute(sqlStatement)
   File "/usr/lib/python2.3/site-packages/pyPgSQL/PgSQL.py", line 3038, in execute
     self.res = self.conn.conn.query(_qstr)
TypeError: query() argument 1 must be string without null bytes, not str
[Tue Jun 21 14:34:46 2005] [error] [client 10.95.100.11] Premature end of script 
headers: /var/www/htdocs/mtest/imagetest.py10.95.100.11] Premature end of script


More information about the Tutor mailing list