Saving Python Pickled objects in MySQL db

血红的双手。 提交于 2020-01-09 19:30:39

问题


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 python object to pickled object

  2. cPickle.loads(pickled_object) # to load back the python object from pickled object

  3. My Django Model Field is Text Field

  4. MySQL db field Type is longblob Attributes binary

  5. MySQL db encoding is utf8_unicode_ci

Unfortunately i am getting following error while loading back python object.

Type Error: ('an integer is required', <type 'datetime.date'>, ('x07xb6x0bx06',))

It seems to me by looking on error value x07xb6x0bx06 this is an encoding problem. Did i miss some important step?? Can any one help me to solve this problem??


回答1:


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']))



回答2:


one more rule: connect to mysql with option charset=utf8?

UPD1: Sometimes it is a good idea to look at the complete SQL query, I usually do it that way:

>>> conn = MySQLdb.connect(**db_params)
>>> "INSERT INTO tbl VALUES (%s)" % conn.literal((your_pickled_item, ))



回答3:


Newtover's answer is probably the correct one, but have a look at

https://github.com/bradjasper/django-pickledfield

It really saved me some time, and can store pretty much anything you want.




回答4:


You can try this, now! django-picklefield https://pypi.org/project/django-picklefield/

To use, just define a field in your model:

>>> from picklefield.fields import PickledObjectField
... class SomeObject(models.Model):
...     args = PickledObjectField()

and assign whatever you like (as long as it's picklable) to the field:

>>> obj = SomeObject()
>>> obj.args = ['fancy', {'objects': 'inside'}]
>>> obj.save()


来源:https://stackoverflow.com/questions/8150078/saving-python-pickled-objects-in-mysql-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!