Read binary data from MySQL database

Stefan Sonnenberg-Carstens stefan.sonnenberg at pythonmeister.com
Thu May 10 10:32:48 EDT 2007


On Do, 10.05.2007, 16:19, Christoph Krammer wrote:
> Hello,
>
> I try to write a python application with wx that shows images from a
> MySQL database. I use the following code to connect and get data when
> some event was triggered:
>
> dbconn = MySQLdb.connect(host="localhost", user="...", passwd="...",
> db="images")
> dbcurs = dbconn.cursor()
> dbcurs.execute("""SELECT imgdata FROM images LIMIT 1""")
> imgstring = dbcurs.fetchone()[0]
> frame.showImage(imgstring)
>
> Within my frame, the following method is defined:
>
> def showImage(self, imgstring):
>   imgdata = StringIO.StringIO()
>   imgdata.write(imgstring)
Use
imgdata.write(imgstring.tostring())
or
imgstring.tofile(imgdata)
>   print imgdata.getvalue()
>   wx.ImageFromStream(imgdata, wx.BITMAP_TYPE_GIF)
>   panel = wx.Panel(self, -1)
>   self.panel = panel
>
> But this does not work. The converter says that the data is not valid
> GIF. When I print the content of imgstring after the database select
> statement, it contains something like this:
>
> array('c', 'GIF89aL\x01=\x01\x85\x00\x00\x00\x00\x00\xff\xff\xff
> \x00\xff\xff\xff[...]\x00\x00;')
>
> When I try to print imgstring[1], the result is "I". So I don't quite
> get what this print result is about and why my input should not be
> valid. The data in the database is correct, I can restore the image
> with tools like the MySQL Query Browser.
>
> Thanks in advance,
>  Christoph
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>




More information about the Python-list mailing list