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')

 

原因:

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

 

解決方法:

使用pymysql.escape_string()方法

如下:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM