cPickle EOF Error

R

Roopesh

I am trying to write xml files which are inside a zip file into the
database.

In the zipfile module the function read returns bytes. What I did was
to make a blob out of the returned bytes and write it to the
database(for this I used cPickle). I also used _mysql's escape_string
which escapes some characters (without this the file content was not
getting written). But once we try to unpickle the content, an EOF
error is getting thrown. When I tried putting the content(xml in
database) to a file without unpickling then the xml file with escape
characters gets printed.

My code is as follows :

def import_cards():
try :
conn =
MySQLdb.connect(host="localhost",port=3306,user="roopesh",passwd="pass",db="mydbs")

cursor = conn.cursor()

cursor.execute("CREATE TABLE card (serial_no int(10),
id varchar(50), data blob, used char(1), ip varchar(20),
date_downloaded datetime)")

z = zipfile.ZipFile('/home/roopesh/public_html/
cards.zip', 'r')

sql = "INSERT INTO card (serial_no, id, data, used)
values (%s, %s, %s, %s)"


for filename in z.namelist() :
bytes = z.read(filename)
data = cPickle.dumps(bytes, 2)
#print data
cursor.execute(sql, (1, filename ,
_mysql.escape_string(data), 'n'))

sql = "SELECT serial_no, id, data, used FROM card"

cursor.execute(sql)
for serial_no, id, data, used in cursor.fetchall() :
print serial_no, id,
cPickle.loads(data.tostring())
# f = open(id ,'wb')
# f.write(data.tostring())
finally :
cursor.execute("DROP TABLE card")
conn.close()

return True

The output is :
--------------------
roopesh@dg:~/public_html$ python cardManagement.py
1 cards/passcard1.xml
Traceback (most recent call last):
File "cardManagement.py", line 136, in ?
import_cards()
File "cardManagement.py", line 28, in import_cards
print serial_no, id, cPickle.loads(data.tostring())
EOFError
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top