473,560 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQL blobs confusion


I've been testing the Cookbook example 8.6 (2002 edition) re using cPickle to
insert and retrieve BLOBs from mySQL, using Python's MySQLdb module.

When I try to cPickle.loads(b lob), I get an error telling me that loads wants a
string, not type array.array. So I go cPickle.loads(b lob.tostring()) instead
and it works.

My question is: has something changed in the Python API since this example was
written?

Here's my version of the code (change commented):

import MySQLdb, cPickle

def cookbook():
connection = MySQLdb.connect ('','root','xxx x','test')
cursor = connection.curs or()

cursor.execute( "CREATE TABLE justatest (name TEXT, ablob BLOB)")

try:
# Prepare some BLOBs to insert in the table
names = 'aramis', 'athos', 'porthos'
data = {}
for name in names:
datum = list(name)
datum.sort()
data[name] = cPickle.dumps(d atum, 1)

# Perform the insertions
sql = "INSERT INTO justatest VALUES (%s, %s)"
for name in names:
cursor.execute( sql, (name, MySQLdb.escape_ string(data[name])) )

# Recover the data so you can check back
sql = "SELECT name, ablob FROM justatest ORDER BY name"
cursor.execute( sql)
for name, blob in cursor.fetchall ():
print name, cPickle.loads(d ata[name]), \
cPickle.loads(b lob.tostring()) # note conversion

finally:
# Done, Remove the table and close the connection
cursor.execute( "DROP TABLE justatest")
connection.clos e()

if __name__ == '__main__':
cookbook()

Kirby

Jul 18 '05 #1
1 3353
Kirby Urner wrote:
I've been testing the Cookbook example 8.6 (2002 edition) re using cPickle to
insert and retrieve BLOBs from mySQL, using Python's MySQLdb module.

When I try to cPickle.loads(b lob), I get an error telling me that loads wants a
string, not type array.array. So I go cPickle.loads(b lob.tostring()) instead
and it works.


see bug:
<http://sourceforge.net/tracker/index.php?func= detail&aid=9758 31&group_id=223 07&atid=374932 >

''Cursor returns array instead of string for blob column type
The standard cursor class ('Cursor') used to return
MySQL blob row types as Python string values. In
version 1.0 it returns arrays containing a single item
with the string 'c' as key and the blob string as the
value.''
BTW i'm using this code:

pickle.loads(ro w['meta'])

to unpickle a blob field (in the 'meta' column)
from a mysql4's MyISAM table and it works as-is.

i'm using both mysqldb 1.0.0 (dev box) and
1.1.6 (on the server).

but i'm using DictionaryCurso r instead of classic
return-as-tuples Cursor.

cheers,
deelan.

--
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#me> a foaf:Person ; foaf:nick "deelan" ;
foaf:weblog <http://www.netspyke.co m/> .
Jul 18 '05 #2

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

Similar topics

1
3649
by: Raaijmakers, Vincent (GE Infrastructure) | last post by:
It is the first time that I use blobs in mysql. Please help me out here..... Using MSSQLdb and python 2.3.4 I was surprised to see how my information was stored in the blob. My goal is to store JPG images in the blob. Well, it stores it but as a string of bytes: 'x0dx0fxffxa3......' So, when reading back that information into my python...
0
1209
by: Dyego Souza do Carmo | last post by:
Hi, I would like to know if you could fix the bug I reported to you some days ago. The error is: "ERROR 1030: Got error 139 from table handler" after running an update command using several blobs. Now I am using mySql 4.0.15 and Linux. If you try to do the same with mySql 4.0.14, the record that is updated is simple deleted.
4
4472
by: Amadeus | last post by:
Hello Everybody! I have a problem with MySQL servers running RedHat 9 (smp kernel 2.4.20) on Intel and MySQL server 4.0.14 (problem also appears on binary distr 4.0.15 and on 4.0.15 I bilt myself from source). I have few big tables with BLOBS and regular table 4.2 and 2.7 Gb respectively, plust several smaller tables. Every time I run query...
74
7903
by: John Wells | last post by:
Yes, I know you've seen the above subject before, so please be gentle with the flamethrowers. I'm preparing to enter a discussion with management at my company regarding going forward as either a MySql shop or a Postgresql shop. It's my opinion that we should be using PG, because of the full ACID support, and the license involved. A...
10
7380
by: John Smith | last post by:
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't recommend storing the image to the filesystem and only keeping a pointer to that in the table. I want to dump the image to a table. My code dumps the...
2
2798
by: Jerry LeVan | last post by:
Hi, I am just getting into large objects and bytea "stuff". I created a small db called pictures and loaded some large objects and then tried to do a restore. Here is how I got the dump. pg_dump -Fc -b pictures > /Users/jerry/desktop/db.comp
4
3785
by: cuyler.jones | last post by:
Hello -- I'm having a heck of a time grabbing a blob ( a jpeg image) from a mySQL database and displaying it on a page. I am able to connect to the database and retrieve the data, however when the page loads, it just spews the binary garbage rather than displaying the image. Here's the code:
10
13390
by: eholz1 | last post by:
Hello Members, I am setting up a photo website. I have decided to use PHP and MySQL. I can load jpeg files into the table (medium blob, or even longtext) and get the image(s) to display without a problem. I am using chunk_split(data) and the base64_encode and base64_decode on the files. I do a select from the database, and then echo the...
3
5367
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped. This function quotes the other characters to make them easier...
0
7635
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
7845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8068
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...
1
7600
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7921
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5459
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2050
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
0
882
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.