0

I want to store an xgboost model to an sql database and read it back:

  1. I pickled my model and wrote it into a varchar field in my db:

    obj = pickle.dumps(mymodel) #write obj to db in a varchar field

  2. When I read the string back from the database into obj and want to pickle.loads(obj[0]), I get this error:

    TypeError: a bytes-like object is required, not a 'str'

How can I unpickle the string (obj) from my database?

sophros
  • 14,672
  • 11
  • 46
  • 75
TomD
  • 1
  • 2
  • This isn't really a language related question. But you could add tag tag for the dbms instead. – jarlh Sep 01 '20 at 08:19
  • Most varchar column types are going to expect characters (with a particular character encoding). A pickle produces a binary type-- so database-wise you'd probably want to store that in a blob type field. If you really want to use a character based field, you'll probably want to base64 encode your pickled object before putting in the database and then base64 decode when retrieving it (see https://stackoverflow.com/questions/30469575/how-to-pickle-and-unpickle-to-portable-string-in-python-3) – clockwatcher Sep 01 '20 at 08:20

0 Answers0