Saving Python Pickled objects in MySQL db

后端 未结 4 1671
北海茫月
北海茫月 2020-12-14 22:55

I am pickling Python Objects in Django and saving it in MySQL db. So far i have followed these simple rules:

  1. cPickle.dumps(object) #to convert

4条回答
  •  一整个雨季
    2020-12-14 23:40

    If you are trying to store the output of cPickle.dumps in a VARCHAR column, then your issue is that you are trying to store a byte-string in a character column. The fix in that case is to encode your object as unicode(base64.encode(cPickle.dumps(myobject))) and then store it.

    Alternatively:

    object2varchar = lambda obj: unicode(base64.encode(cPickle.dumps(obj)))
    store(object2varchar([1, 'foo']))
    

提交回复
热议问题