Python:ODBC 异常处理

发布于 2024-07-21 00:30:38 字数 656 浏览 3 评论 0原文

我需要在我的应用程序中识别表是否不存在或没有行来采取适当的操作。 我可以分别捕获这两个错误吗?

>>>cursor.execute("delete from TABLE")

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
dbi.internal-error: [IBM][CLI Driver][DB2] SQL0100W  No row was found for FETCH, UPDATE or DELETE; or the result of a query is an empty table.  SQLSTATE=02000
 in EXEC

或者

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
dbi.program-error: [IBM][CLI Driver][DB2] SQL0204N  "SK77531.TCS_EXCEPTIONS" is an undefined name.  SQLSTATE=42704
 in EXEC

I need to recognize in my application whether table doesn't exist or has no rows to take appropriate action. Can I catch this two errors separately ?

>>>cursor.execute("delete from TABLE")

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
dbi.internal-error: [IBM][CLI Driver][DB2] SQL0100W  No row was found for FETCH, UPDATE or DELETE; or the result of a query is an empty table.  SQLSTATE=02000
 in EXEC

OR

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
dbi.program-error: [IBM][CLI Driver][DB2] SQL0204N  "SK77531.TCS_EXCEPTIONS" is an undefined name.  SQLSTATE=42704
 in EXEC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独陪着我 2024-07-28 00:30:38

来自 Python 文档

一条 try 语句可以有多个 except 子句,以指定不同异常的处理程序。

例如:

try:
    do_something_crazy
except AttributeError:
    print 'there was an AttributeError'
except NameError:
    print 'there was a NameError'
except:
print 'something else failed miserably'

最后一个 except 在这里充当包罗万象的角色,并且仅在发生与 AttributeError 或 NameError 不同的异常时才执行。  在生产代码中,最好避免使用此类包罗万象的 except 子句,因为通常,只要发生意外的错误,您就会希望代码失败。

在您的特定情况下,您需要导入可以从 dbi 模块引发的不同异常,以便您可以在不同的 except 子句中检查它们。

就像这样:

# No idea if this is the right import, but they should be somewhere in that module
import dbi

try:
    cursor.execute("delete from TABLE")
except dbi.internal-error:
    print 'internal-error'
except dbi.program-error:
    print 'program-error'

正如您将在上面的文档页面中看到的,您可以选择在每个 except 子句中包含其他属性。 这样做将使您可以访问实际的错误对象,当您需要区分同一类的两个不同异常时,这可能是必要的。 即使您不需要如此精细的区分,比我上面概述的进行更多检查仍然是一个好主意,以确保您确实正在处理您认为正在处理的错误。

关于 try/ except 的所有说明和完成,我真正建议的是在尝试与表交互之前在您使用的数据库库代码中搜索一个方法来检查表是否存在。 当您处理需要检查和清理的外部输入时,结构化尝试/例外非常有用,但是围绕数据库表的暂定存在进行防御性编码听起来像是稍后会反过来咬您一口的东西。

From the Python documentation:

A try statement may have more than one except clause, to specify handlers for different exceptions.

For example:

try:
    do_something_crazy
except AttributeError:
    print 'there was an AttributeError'
except NameError:
    print 'there was a NameError'
except:
print 'something else failed miserably'

The last except acts as a catch-all here, and is only executed if an exception different than an AttributeError or NameError occurs.  In production code it's best to steer clear from such catch-all except clauses, because in general you'll want your code to fail whenever an error that you didn't expect occurs.

In your specific case you'll need to import the different exceptions that can be raised from the dbi module, so you can check for them in different except clauses.

So something like this:

# No idea if this is the right import, but they should be somewhere in that module
import dbi

try:
    cursor.execute("delete from TABLE")
except dbi.internal-error:
    print 'internal-error'
except dbi.program-error:
    print 'program-error'

As you'll see in the above-lined documentation page, you can opt to include additional attributed in each except clause.  Doing so will let you access the actual error object, which might be necessary for you at some point when you need to distinguish between two different exceptions of the same class.  Even if you don't need such a fine level of distinction, it's still a good idea to do a bit more checking than I outlined above to make sure you're actually dealing with the error you think you're dealing with.

All that said and done about try/except, what I'd really recommend is to search for a method in the database library code you're using to check whether a table exist or not before you try to interact with it.  Structured try/excepts are very useful when you're dealing with outside input that needs to be checked and sanitized, but coding defensively around the tentative existence of a database table sounds like something that's going to turn around and bite you later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文