473,516 Members | 3,456 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read binary data from MySQL database

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

May 10 '07 #1
2 6291
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
May 10 '07 #2
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

May 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
21897
by: Thierry | last post by:
Hello, I do have a mysql database with a BLOB column. What is the proprest way to read this column ? Thx, Thierry
2
4123
by: Sune | last post by:
Hi, Does anyone know a website with a guide on how to upload binary data to a MySQL database using ASP? I have a script that i spent a lot of hours on getting to work, but after i upgraded my MySQL server it doesn't work anymore :( I'm just getting an error from the odbc driver (upgraded from 2.5X odbc driver to 3.51X):
4
4009
by: Holger Marzen | last post by:
Hi all, AFAIK it is possible for columns to be very large, up to about 2 GB. Are there any hints or experiences about storing binary data (jpg-images, pdf-documents) in PostgrreSQL with or without the complicated lo-stuff? Of course it's in many cases a good approach to store those files simply in the file system but there's always a risk...
0
1637
by: Richard Marsden | last post by:
I'm having a lot of trouble writing large chunks of binary data (tests are in the range of 16-512K, but we need support for large longblobs) to MySQL using ODBC. Database is local on a W2K system, but I have to support all modern Windows systems, and a variety of ODBC configurations. (I'll be testing against multiple ODBC databases soon - but...
5
3359
by: lawrence k | last post by:
I'm a little weak on my basic I/O. Help me out please. Is it right to say that I can just open any file with file(), get it as a string, and then store in a MySql database, in, say, a MediumText field? I realize that MySql supports binary fields, but my current MySql schema does not have any binary fields, and I'm wondering if I can store...
3
8469
by: Me Alone | last post by:
Hello: I am trying to edit some C code I found in "The definitive guide to using, programming, and administering MySQL" by Paul DuBois. This C client program connects and then segfaults when the function load_image is called. Would anyone be able to point me to what I might be doing wrong? Thanks in advance, C Newbie
4
7217
by: =?ISO-8859-1?Q?Hans_M=FCller?= | last post by:
Good morning folks, 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(), ))
3
5993
by: ist | last post by:
Hi, I am trying to get (and transfer over ASP.NET) some encrypted data from some MySQL fields. Since the data contains many unicode characters, I tried to get the data as a series of ASCII values, transfer those numeric values over ASP.NET. I had no problem doing this on my local computer, by getting the field with "cast(field as BINARY)"...
0
1335
by: canistel | last post by:
Hi, I have a little python webservice that I created, and in one of the methods I need to store some binary data that was "posted"... I want to do something like this, but it doesn't work. username = form.get("username", "") message = form.get("message", "") attachment = form.get("attachment", None) .... c.execute("""INSERT INTO Message...
0
7273
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7574
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3265
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3252
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.