cx_oracle错误处理问题(cx_oracle Error handling issue)

2019-11-01 05:12发布

我试图在执行以执行cx_Oracle以下查询,但得到以下错误:

    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()

Answer 1:

我认为这是一个更好的方式来做到这一点。 我不知道什么是你的代码的问题没有得到更多的细节,但尝试了这一点,我希望这将是有益的。

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 Error handling issue