March 27th, 2004, 06:02 PM
Saving pickled binary data
I need to save pickled data in MySQL.
In order to do that :
1. I am using pickling using pickle.py and the binary switch, switched
from pickle import *
x = dumps(obj,bin=1)
2. And then trying executing SQL againt MySql DB
sqlToCommit = "Update sess SET SessPageInfo = " '+x+' "
where sessID ='123'
Unfortunately, sometimes , after serialization, I get single quote or double quotes in x.
which makes the SQL invalid for MySql
( MySql "thinks" its a beggining / end of a string )
any suggestions how to do that?
March 28th, 2004, 09:57 AM
The MySQLdb module has several conversion functions that will solve this problem. The most useful is escape_string(s) which will escape special characters such as single and double quotes, so it can safely be used in a SQL statement.
Incidentally, this is a recipe in the O'Reilly Python Cookbook, which I highly recommend to anyone doing serious Python development.
Dave - The Developers' Coach
March 29th, 2004, 08:15 PM
You can also use a raw string to escape data in MySQL statments although I seem to remember the MySQL module now calls escape string austomatically . There should be a thread around here somewhere on this if you search the forums.