Call the MySQLdb module in Python to insert data information, assuming that the input information data is:
Hello'world'!
Both single and double quotes are included
The general INSERT statement is
" INSERT INTO TB (MY_STR) VALUES ('%s ') " % (data) cursor.execute (SQL)
There is also a quotation mark outside of%s in values ('%s '), which matches the quotation mark in data to cause a content error
Workaround: escape character
Change data to the following form, and then insert the database correctly
Hello\'world\ '!
The escape functions in Python are as follows:
deftransfercontent (self, content): String="" forCinchcontent:ifc = ='"': String+='\\\"' elifc = ="'": String+="\\\ '" Else: String+=CreturnString
To add three \, this is because \ \ will be in the function of \,\ ' will escape to ', the two together to leave in the string \ ', and then SQL read to recognize that this is escaped
In Python, the following two types of notation are the same
a=""a=""
"Python" SQL statement insert solution with both single and double quotes in the contents