473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(blob), I get an error telling me that loads wants a
string, not type array.array. So I go cPickle.loads(blob.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','xxxx','test')
cursor = connection.cursor()

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(datum, 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(data[name]), \
cPickle.loads(blob.tostring()) # note conversion

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

if __name__ == '__main__':
cookbook()

Kirby

Jul 18 '05 #1
1 3342
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(blob), I get an error telling me that loads wants a
string, not type array.array. So I go cPickle.loads(blob.tostring()) instead
and it works.


see bug:
<http://sourceforge.net/tracker/index.php?func=detail&aid=975831&group_id=22307&at id=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(row['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 DictionaryCursor 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.com/> .
Jul 18 '05 #2

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

Similar topics

1
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...
0
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...
4
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...
74
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...
10
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...
2
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. ...
4
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...
10
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...
3
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.