记录昨天遇到的问题以及解决方案。
昨天再弄爬虫爬取数据想存入数据库的时候,突然抛出pymysql.err.ProgrammingError: (1064,******)明明MySQL语句是对的,但是怎么都运行不成功。错误提示就是他:
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 'howtouse)\nVALUES('Fondant Fluidealiste Conditioner', 'Kerastase', 'dant Fluideal' at line 1")
谷歌了才知道python里面存数据库的时候要防止转义。所以需要将代码添加一句:
pymysql.escape_string(data_dict['desscribe'])
如:
#插入产品信息
insert_good_sql = """
INSERT INTO T_GOOD(good_name, good_type, img_src, good_description, how_to_use,
volumetric, price,sale, spider_time)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
values = (pymysql.escape_string(data_dict['good_name']),
pymysql.escape_string(data_dict['good_type']),data_dict['img_src'], pymysql.escape_string(data_dict['good_description']), data_dict['how_to_use'],pymysql.escape_string(data_dict['volumetric']), pymysql.escape_string(data_dict['price']),data_dict['sale'], datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))