0

I have a large object that is fairly dynamic that I want to serialize and store in my sqlite database. Using the pickle module, I made it into a string but found I had to do a string replace on it to change the single quotes out in order to store it to the db. When I take it out, I just undo that. I am new to using pickle and sqlite and this at least works, but is this really the right way to go about it or is there a correct way to handle this that I am not seeing?

menu = pickle.dumps(menuOb)
menu = menu.replace("'", "''")
#sql statement stuff
0

1 Answer 1

2

Don't replace any characters, just insert with a SQL parameter:

cursor.execute('INSERT INTO sometable VALUES (?, ?)', (id, pickle.dumps(menuOb))

SQL parameters take care of correct quoting for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, I am definitely a SQL noob. What I was doing just seemed silly and this makes a lot more sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.