for row in cursor.fetchall():
currentid = str(row[0])
links = soup.findAll('a', {'class':'lightboxImage'})
for link in links:
file_name = urllib2.unquote(url).decode('utf8').split('/')[-1]
#print ("<img src="http:" + str(link['href'] + ""</img>"))
cursor.execute("INSERT INTO `Images`(`ImageURL`, `ProductID`) VALUES (" + """ + file_name + """ + " , " + currentid)
这是我运行它时得到的错误。
root@eBayFileServer:~# python scrape.py
Traceback (most recent call last):
File "scrape.py", line 102, in <module>
cursor.execute("INSERT INTO `Images`(`ImageURL`, `ProductID`) VALUES (" + """ + file_name + """ + " , " + currentid)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.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 '' at line 1")
我试过各种各样的方法,比如改变数据周围的引号,但我就是不明白
try this:
cursor.execute("INSERT INTO `Images`(`ImageURL`, `ProductID`) VALUES (" + "'" + file_name + "'" + " , " + currentid+");")
你可以使用单引号,这样你就不需要转义双引号并关闭VALUES