简体   繁体   中英

cx_oracle Error handling issue

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM