cx_oracle错误处理问题 [英] cx_oracle Error handling issue
本文介绍了cx_oracle错误处理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在cx_Oracle中执行以下查询,但是在执行时遇到以下错误:
I'm trying to execute the following query in cx_Oracle but get the following error while executing:
print 'Error.code =', error.code
AttributeError: 'str' object has no attribute 'code'
代码:
try:
conn.exec("Select * from table1")
except cx_Oracle.DatabaseError, ex:
error, = ex.args
print 'Error Inserting Field Base'
print 'Error.code =', error.code
print 'Error.message =', error.message
print 'Error.offset =', error.offset
conn.rollback()
推荐答案
我认为这是一种更好的方法。我不确定您的代码有什么问题,而没有获取更多详细信息,但是请尝试一下,希望这一点有用。
I think this is a better way to do it. I am not sure what is the issue with your code without getting more details but try this out and I hope this shall be useful.
In [10]: connstr="%s/%s@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%d))(CONNECT_DATA=(SERVICE_NAME=%s)))" % tuple(db[0:5])
In [11]: try:
....: conn = cx_Oracle.connect(connstr)
....: query = 'select * from table_name limit 1;'
....: curs = conn.cursor()
....: curs.arraysize=50
....: curs.execute(query)
....: curs.close()
....: conn.close()
....: except cx_Oracle.DatabaseError, ex:
....: error, = ex.args
....: print 'Error.code =', error.code
....: print 'Error.message =' , error.message
....: print 'Error.offset =', error.offset
....: conn.rollback()
....:
Error.code = 933
Error.message = ORA-00933: SQL command not properly ended
Error.offset = 31
这篇关于cx_oracle错误处理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文