Loading a pickle that's stored as a row entry?

Viewed 16

I am having an issue loading some pickled objects that are stored in an SQL table.

I have a table, tbl, that looks like the following,

ModelId ModelObject
1 0x800363736B6C6561726E2E6C696E6561725F6D6F64656C2E636F6F7264696E6ACAEE3FABCEEFD58889F13F3C19DFC8235DF03F3E6FDAB870B5F43F1C31AB7E93BEF23F5F5223C65948EF3FD08961D
2 0x800845242158652135486524156ER...

In the column 'ModelObject', the strings represent some pickles that were stored as an entry using the function pickle.dumps. I am trying to load the first model object from this SQL table and I cannot seem to get it to load properly. Below is some code that I have tried using to load the pickles but won't work.

conn = pyodbc.connect('Driver={SQL Server};'
                           'Server=MyServer;'
                           'Database=MyDatabase;'
                           'Trusted_Connection=yes;')

sql_query = "SELECT * FROM [MyDatabase].[dbo].[models_data]"

tbl = pd.read_sql(sql_query, conn)

model_1 = pd.read_pickle(tbl.iloc[0]['ModelObject'])
model_1 = pickle.load(tbl.iloc[0]['ModelObject'])

The two last lines both raise different errors, neither working to load the pickles. pd.read_pickle gives the error 'AttributeError: 'bytes' object has no attribute 'seek'' and pickle.load gives the error ''TypeError: file must have 'read' and 'readline' attributes''.

Can anyone help with this?

1 Answers

Since you have the pickle as a string, you should use pickle.loads, which is the string loading version of pickle.

model_1 = pickle.loads(tbl.iloc[0]['ModelObject'])
Related