在我学会爬行并能够将数据插入到mysql之后
Insert multiple Scrapy data into mysql
当我插入几个爬行数据时,我发现了另一个问题,我在mysql中的数据变得很大,因为有太多冗余数据(重复数据)
我试着像在php中一样,用WHERE NOT EXIST条件做插入操作,而不是在python中工作。
item1 = quote.css('h2.post-title > a::text').extract_first()
item2 = quote.css('div.entrytext > p::text').extract_first()
item3 = quote.css('div.up-bottom-border > p.postmetadata >
a::text').extract()
items3 = ', '.join(item3)这是我的sql
mycursor.execute("INSERT INTO berita (judul, isi, tag) VALUES (%s, %s, %s) WHERE NOT EXISTS (SELECT judul FROM berita WHERE judul like '%s')", (item1, item2, items3, item1))我希望有人帮助我,因为错误保持说
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'WHERE NOT EXISTS (SELECT judul FROM berita WHERE judul like ''Tanah Longsor di K' at line 1发布于 2019-02-20 16:20:44
错误是关于重复的引号。这是因为您为查询字符串中的最后一个变量添加了自己的'引号。这些引号会自动添加,因此请删除它们:
WHERE NOT EXISTS (SELECT judul FROM berita WHERE judul like %s)
https://stackoverflow.com/questions/54779902
复制相似问题