How to read a binary file into a mysql table

Benoit benoit.barberousse at gmail.com
Sat Dec 15 05:51:20 EST 2007


On Dec 14, 5:41 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Fri, 14 Dec 2007 12:19:41 -0300, Hans Müller <HeinT... 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

I was suprised at what I could stick into a MySQL database.  Also, you
might wanna compress the binary for database performance.



More information about the Python-list mailing list