python mysql 插特殊字符,1064, "You have an error in your SQL syntax; check the manual that corresponds to

 
 
  • 代码片段:
sql = """
            insert into wbuser (uid,screen_name,gender,fans_no,description,is_followed) VALUES ('%s','%s','%s','%d','%s','%s')"""
        try:
            mysql = Mysql()
            #~If your dreams don't scare you, they are not big enough!~
            mysql.execute_no_query(sql % (uid,screen_name,gender,fans_no,description,is_followed))
        except Exception as e:
            print(e)
        finally:
            mysql.db_close()  

  • 插入时出错内容:

~If your dreams don't scare you, they are not big enough!~

  • 错误提示
(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't scare you, they are not big enough!~','0')' at line 1")
  • 解决:对字符串做处理
import pymysql
mysql.execute_no_query(sql % (pymysql.escape_string(uid),pymysql.escape_string(screen_name),pymysql.escape_string(gender),fans_no,pymysql.escape_string(description),pymysql.escape_string(is_followed)))


猜你喜欢

转载自blog.csdn.net/qq284489030/article/details/80539224