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
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