不积跬步,无以至千里;不积小流,无以成江海。——荀子

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

代码:

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"]))

 

posted on 2018-01-04 16:56  hejunlin  阅读(29425)  评论(0编辑  收藏  举报