Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Read binary data from MySQL database

Reply
Thread Tools

Read binary data from MySQL database

 
 
Christoph Krammer
Guest
Posts: n/a
 
      05-10-2007
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

 
Reply With Quote
 
 
 
 
Carsten Haese
Guest
Posts: n/a
 
      05-10-2007
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


 
Reply With Quote
 
 
 
 
Stefan Sonnenberg-Carstens
Guest
Posts: n/a
 
      05-10-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
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
How Can I read the Binary data from Database Husam ASP .Net 1 12-25-2007 02:43 PM
using mysql client to read a file to create a table in a database in a mysql server julian Ruby 8 04-06-2006 10:29 AM
I saved password into the database with "binary" data type from GetByte method. How can I to compare the password that between database and transfered from the page? Benny Ng ASP .Net 3 01-25-2006 06:49 AM



Advertisments