SQLAlchemy mysql insert a special character error

SQLAlchemy insert special characters operations

Quotes error

You have encountered an error when inserting a string of quotes when using SQLAlchemy execute sql statements
such as the following code in sql after splicing into a string of comments, this time, of course insert mysql error.

content = 'test"hhhh",'
sql = 'insert into comment(content) values("%s")'%content
#insert into comment(content) values("test"hhhh",")
execute_sql(sql)

image.png

Solution:
1. escaped as their own quotes with a backslash

content = content.replace('"','\\"')

2. Third-party dependencies

import pymysql
content =pymysql.escape_string(content )  

pymysql.escape_string can escape character
image.png

Colon error

Recent problems encountered in a giant pit string begins with a colon also can not be saved, or in the presence of a string of Chinese punctuation colon can not be saved. (Specific reasons not found, this operation seems to be treated as an object in the SQLAlchemy)
image.png

content = ":test"
content = ",:test"
sql = 'insert into comment(content) values("%s")'%content
execute_sql(sql)

Guess you like

Origin www.cnblogs.com/i-love-python/p/11593501.html