I am receiving data into an REST API, and I want to insert it as XML code into a database. When I later read the record from the database, I expect well-formed XML code.
data=str(request.data)
cur=mydb.cursor()
currentDT = datetime.datetime.now()
val=data.replace("'","''")
cur.execute("insert into MyXMLApi(dateofInsertion,xmlData) values('%s','%s')"%(str(currentDT),val))
mydb.commit()
This is what I expect to see in the database:
"<note>
Don't forget me this weekend!
</note>"
However, this is what I actually get:
'b"<note>
Don''t forget me this weekend!
</note>"'
So I have three issues here.
- I have to deal with single quotes in the XML code.
- It should be stored as proper XML code.
- When I read from the database, I can't get the right code.