0

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

1 回答 1

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
于 2012-11-19T22:49:39.187 回答