pymysql.err.ProgrammingError: 1064 (Python字符串轉義問題)

 

代碼:

sql = """INSERT INTO video_info(video_id, title) VALUES("%s","%s")""" % (video_info["id"],video_info["title"])

 

問題:

pymysql.err.ProgrammingError: (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 \'Recipe" of Machine Learning","https://i.ytimg.com/vi/DkgJ_VkU5jM/hqdefault.jpg",\' at line 4')mysql

 

緣由:

當video_info["title"]裏面含有引號時,可能會發生如上錯誤。sql

 

解決方法:

使用pymysql.escape_string()方法ide

以下:spa

sql = """INSERT INTO video_info(video_id, title) VALUES("%s","%s")""" % (video_info["id"],pymysql.escape_string(video_info["title"]))
相關文章
相關標籤/搜索