This is my query.
cursor2.execute("update myTable set `"+ str(row[1]) +"` = \"'" + str(row[3]) +"'\" where ID = '"+str(row[0])+"'")
It is failing when row values have double quotes “some value”. How do I escape all special characters?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here is an example:
Update
Here is a brief commentary:
Always a good idea to escape anything that goes into a query. In this case we are dynamically adding a column name and hence it has to be escaped before the query is executed.
I am forming the query here. I am trying to achieve two things: (1) form a query with column name populated using the
columnvariable declared in the previous line (2) add placeholders that will be filled in by actual parameters during query execution.The snippet
dict(column = column)is actually another way of creating the dictionary{'column': column}. This is made possible using the dict constructor. I don’t want tofill in the other place holders just yet so I escape them using two percentage signs (
%%).Finally execute the query. If you print query before executing you’ll see the string
update myTable set column_name = %s where ID = %s.