gpt4 book ai didi

mysql - 使用带有扭曲的 adbapi 和 scrapy 的 mysql 进行异常处理

转载 作者:行者123 更新时间:2023-11-29 21:46:42 24 4
gpt4 key购买 nike

我正在使用this scrapy管道。如果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 方法,但也没有被调用。我如何捕获并处理此类错误?

最佳答案

只需要用 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)

关于mysql - 使用带有扭曲的 adbapi 和 scrapy 的 mysql 进行异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34054094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com