#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    71
    Rep Power
    11

    Saving pickled binary data


    Hi.
    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
    to on:
    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'
    ='"+sessID+"'"
    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?
    Thanks
    roy
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo