I have stored a class object as a pickle in an SQLite DB.
Below is code for the file pickle.py
sqlite3.register_converter("pickle", pickle.loads)
sqlite3.register_adapter(list, pickle.dumps)
sqlite3.register_adapter(set, pickle.dumps)
class F:
a = None
b = None
def __init__(self) -> None:
pass
df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
f = F()
f.a = df
f.b = df.columns
data = pickle.dumps(f, protocol=pickle.HIGHEST_PROTOCOL)
sqliteConnection = sqlite3.connect('SQLite_Python.db')
cursor = sqliteConnection.cursor()
print("Successfully Connected to SQLite")
DATA = sqlite3.Binary(data)
sqlite_insert_query = f"""INSERT INTO PICKLES1 (INTEGRATION_NAME, DATA) VALUES ('James',?)"""
resp = cursor.execute(sqlite_insert_query,(DATA,))
sqliteConnection.commit()
After that, I am trying to fetch the pickle from the DB. The pickle is stored in a pickle datatype column which I had registered earlier on SQLite in file retrieve_pickle.py.
cur = conn.cursor()
cur.execute("SELECT DATA FROM PICKLES1 where INTEGRATION_NAME='James'")
df = None
rows = cur.fetchall()
for r in rows[0]:
print(type(r)) #prints <class 'bytes'>
df = pickle.loads(r)
But it gives me an error
File "/Users/ETC/Work/pickle_work/picklertry.py", line 34, in select_all_tasks
df = pickle.loads(r)
AttributeError: Can't get attribute 'F' on <module '__main__' from '/Users/rusab1/Work/pickle_work/picklertry.py'>
I was trying to store a class object in a pickle column in sqlite after registering pickle.loads as a pickle datatype. I kept the object successfully and was able to retrieve it from DB but when I try to load it back so that I can access the thing and attributes it gives me an error.