【问题标题】:mysqldb programming Errormysqldb编程错误
【发布时间】:2016-01-07 22:27:41
【问题描述】:

我在尝试使用 MySQLdb 插入表时遇到错误,但无法弄清楚。我查找了如何捕获错误,但是当我添加代码来捕获它时,当我尝试使用 raise e 而不是 print e 并且当我尝试 print e 时,我没有得到任何额外的信息。我需要一些帮助来获取错误以提供更多详细信息。我将包含整个代码,以防有人很棒,一开始就知道我哪里出错了。然后我什至不关心错误捕获。好的,是的,因为我相信我很快就会再次需要它。哈哈。我实际上有 8 列需要更新。我正在尝试使用消除过程,一次只尝试几个。像 UPC、Name 和其他几个这样的插入很好,但 Modl、Path 和 Desc 会导致错误。任何帮助都将不胜感激和感谢。

# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from __future__ import print_function
from datetime import date, datetime, timedelta
import MySQLdb
#from scrapy.extensions import DropItem
#from bff.items import ItemInfo

class mySQLPipeline(object):
    def process_item(self, item, spider):

        Product = item['ProdName']      
        Path = item['ProdPath']
        UPC = item['ProdUPC']
        Modl = item['ProdModel']
        Desc = item['ProdDesc']
        Price = item['ProdPrice']
        Stock = item['InStock']
        #Ships = item['Ships']
        Name = item['StoreName']
#Not Failing during insert    Product, Price, Stock, Name
#FAILing during insert   Modl, Path, Desc, 



        db = MySQLdb.connect(user='****', passwd='****',
                                      host='127.0.0.1',
                                      port=****,
                                      db='****')
        cursor = db.cursor()
 #       add_Product = ("INSERT INTO walmart_products (ProdName, StoreName) VALUES (%s, %s,)", Product, Name,)
 #       add_Product = ("INSERT INTO walmart_products, (ProdName)"
 #                      "VALUES (%s)", (Name))
 #                      "VALUES (%(Name)s)")
        add_Product = ("INSERT INTO walmart_products "
                        "(InStock, StoreName) "
                        "VALUES (%s, %s)")
                        #item['Ships'],

        data_Product = (Stock, Name)                                        

        #Add new product
        #try:
        cursor.execute(add_Product, data_Product)

        #except MySQLdb.IntegrityError, e: 
            #print e 

          # handle a specific error condition
        #except MySQLdb.Error, e:
            #raise e
          # handle a generic error condition
        #except MySQLdb.Warning, e:
            #print e        
          # handle warnings, if the cursor you're using raises them      
        #except MySQLdb.ProgrammingError, e:
            #print e

        # Make sure data is committed to the database
        db.commit()

        cursor.close()
        db.close()
        return item

【问题讨论】:

  • 您使用[] 将值放入列表中,而不是()data_Product = [Stock, Name]
  • 感谢巴尔玛的回复。我试了一下,得到了同样的错误。就像你说的那样,我再次用 [] 替换它们,但仍然出现错误。很模糊,但在这里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 \'), ("\'http://www.godaddy.com/ip/5026447\'",))\' at line 1')
  • 这对我不起作用,但感谢您的回复。我弄清楚发生了什么。这实际上是几件事。其中一个我将列设置为 varchar(200) 并且获得了超过 2400 个字符,因此增加它可以解决这个问题。在我没有转义的 URL 上,这导致了错误。再次感谢。

标签: python mysql mysql-python


【解决方案1】:

我知道发生了什么。这实际上是几件事。其中一个我将列设置为 varchar(200) 并且获得了超过 2400 个字符,因此增加它可以解决这个问题。在我没有转义的 URL 上,这导致了错误。再次感谢。 – 尼克刚刚编辑

【讨论】:

  • Path = str(item['ProdPath']) Path = MySQLdb.escape_string(Path)
  • 把它放在答案中,而不是评论中。
猜你喜欢
  • 1970-01-01
  • 2017-01-25
  • 2011-10-20
  • 2023-04-11
  • 2016-01-11
  • 2015-12-27
  • 1970-01-01
  • 2018-10-08
  • 2012-07-11
相关资源
最近更新 更多