【问题标题】:Exception handling using mysql with twisted adbapi and scrapy使用带有扭曲 adbapi 和 scrapy 的 mysql 进行异常处理
【发布时间】:2016-03-07 08:44:47
【问题描述】:

我正在使用thisscrapy 管道。如果 insert_record 函数中的 sql 有任何错误,它会静默失败。例如,如果列名拼写错误,像这样

def _insert_record(self, tx, item):
        print "before tx.execute"
        result = tx.execute(
        """ INSERT INTO table(col_one, col_typo, col_three) VALUES (1,2,3)""" 
        )
        print "after tx.execute"
        if result > 0:
            self.stats.inc_value('database/items_added')

那么在“执行前”之后没有任何输出。有一个 handle_error 方法,但也没有调用。如何捕捉和处理此类错误?

【问题讨论】:

    标签: mysql python-2.7 scrapy twisted


    【解决方案1】:

    只需要用 try...except 包围它

    try:
        result = tx.execute(
        """INSERT INTO table(col_one, col_typo, col_three) VALUES (1,2,3)"""
        )
    except Exception,e: 
        print str(e) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多