cx_oracle错误处理问题

时间:2012-11-19 20:53:10

标签: python cx-oracle

我正在尝试在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()

1 个答案:

答案 0 :(得分: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