How to read a binary file into a mysql table

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Dec 14 18:41:24 EST 2007


En Fri, 14 Dec 2007 12:19:41 -0300, Hans Müller <HeinTest at web.de> escribió:

> I cannot read a binary file into a mysql database. Everything I tried  
> did not succeed.
>
> What I tried (found from various google lookups...) is this:
>
> con = MySQLdb.connect(to server)
> cur = con.cursor()
>
> cur.execute("insert into data values('file1', %s)", (open("test.jpg",  
> "rb").read(), ))

Try wrapping the file contents with a Binary object (untested):

data = MySQLdb.Binary(open("test.jpg","rb").read())
cur.execute("insert into data values('file1', %s)", (data,))

-- 
Gabriel Genellina




More information about the Python-list mailing list