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 1 3358
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/> . This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 environment, it seems that I need to translate the value back to the original binary format?
...
|
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.
|
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
against this tables MySQL uses all available memory on server (I have
3Gb RAM on server) and it...
|
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 consultant my company hired before
bringing me in is pushing hard for MySql, citing speed and community...
|
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 data into the table, however, I get
the following error when trying to view the image "the image ......
| |
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
|
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:
|
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 image (with
header(Content Type: image/jpeg)
and the decoded image displays fine. Yes, I have...
|
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 to read in
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |