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")
我尝试了各种方法,例如更改数据周围的引号,但我就是想不通。
最佳答案
试试这个:
cursor.execute("INSERT INTO `Images`(`ImageURL`, `ProductID`) VALUES (" + "'" + file_name + "'" + " , " + currentid+");")
您可以使用单引号,这样您就不需要转义双引号并关闭 VALUES 中的括号
关于python - Python 的 MYSQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25424891/