Read binary data from MySQL database

Discussion in 'Python' started by Christoph Krammer, May 10, 2007.

  1. 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)
    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
     
    Christoph Krammer, May 10, 2007
    #1
    1. Advertising

  2. On Thu, 2007-05-10 at 07:19 -0700, 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)
    > 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;')


    That means that imgstring is not a string, it's an array of characters.
    Observe:

    >>> import array
    >>> a = array.array('c', 'Blahblahblah')
    >>> print a

    array('c', 'Blahblahblah')
    >>> str(a)

    "array('c', 'Blahblahblah')"
    >>> a.tostring()

    'Blahblahblah'

    Calling write() with an object that's not a string will implicitly call
    str() on that object and write the result of that call to the file. Try
    imgdata.write(imgstring.tostring()) to extract the string data from the
    array.

    Hope this helps,

    --
    Carsten Haese
    http://informixdb.sourceforge.net
     
    Carsten Haese, May 10, 2007
    #2
    1. Advertising

  3. 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
    >
    >
     
    Stefan Sonnenberg-Carstens, May 10, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JL
    Replies:
    0
    Views:
    1,178
  2. Doug
    Replies:
    3
    Views:
    4,413
    Bruce Barker
    Nov 4, 2005
  3. Husam
    Replies:
    1
    Views:
    585
    Peter Bromberg [C# MVP]
    Dec 25, 2007
  4. Jeffrey H. Coffield
    Replies:
    1
    Views:
    1,936
  5. julian
    Replies:
    8
    Views:
    516
    Avatar
    Apr 6, 2006
Loading...

Share This Page