python - Python 的 MYSQL 错误

标签 python mysql

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/

相关文章:

Mysql 函数

python - 大型数据集 python (scipy) 的 interp1d 性能问题的线性插值

php - 转换 mysql_result 困难

php - php my sql中下拉菜单的动态变化

mysql - 需要有关 LIMIT 子查询的帮助

mysql - 手动将 MySQL 数据库文件放入数据文件夹 - WAMP - XAMPP

python - 使用动态属性名称调用 GAE NDB 查询

python - 如何在 Sphinx 生成的文档中添加 'previous chapter' 和 'next chapter' 链接?

python - 如何检测图像中是否存在次要对象

python - 获取 Python 文件上传到 Azure 的进度