Python:ODBC 异常处理
我需要在我的应用程序中识别表是否不存在或没有行来采取适当的操作。 我可以分别捕获这两个错误吗?
>>>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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 Python 文档:
例如:
最后一个 except 在这里充当包罗万象的角色,并且仅在发生与 AttributeError 或 NameError 不同的异常时才执行。 在生产代码中,最好避免使用此类包罗万象的 except 子句,因为通常,只要发生意外的错误,您就会希望代码失败。
在您的特定情况下,您需要导入可以从 dbi 模块引发的不同异常,以便您可以在不同的 except 子句中检查它们。
就像这样:
正如您将在上面的文档页面中看到的,您可以选择在每个 except 子句中包含其他属性。 这样做将使您可以访问实际的错误对象,当您需要区分同一类的两个不同异常时,这可能是必要的。 即使您不需要如此精细的区分,比我上面概述的进行更多检查仍然是一个好主意,以确保您确实正在处理您认为正在处理的错误。
关于 try/ except 的所有说明和完成,我真正建议的是在尝试与表交互之前在您使用的数据库库代码中搜索一个方法来检查表是否存在。 当您处理需要检查和清理的外部输入时,结构化尝试/例外非常有用,但是围绕数据库表的暂定存在进行防御性编码听起来像是稍后会反过来咬您一口的东西。
From the Python documentation:
For example:
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:
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.