user1050619
user1050619

Reputation: 20856

oracle error code handling cx_oracle

I'm trying to accessing Oracle from Python using cx_ORacle.

I have a select statement that returns no rows ie; NO_DATA_FOUND..and this is how Im handling the error condition.

When I execute this piece of code, the error NO_DATA_FOUND is not catched by the cx_oracle.error or cx_oracle.Databaseerror or cx_oracle.Warning..

How do I handle the NO_DATA_FOUND condition?

code.py

def DetermineNames(self): 
    sql = """select NAME from EMP_TAB where fd_fle_id = %s"""%(self.fileid)
    try:
         self.cursor.execute(sql)
         result = self.cursor.fetchall()
         for row in result:
               print('row',row)
    except cx_Oracle.Error as e:
        print("Error:Unable to determine the RAW_OBJ_NAME: Object Name:%s, Function Name:%s  Error:%s")%(self.__class__.__name__,sys._getframe().f_code.co_name,
        str(e).strip())                        
    except cx_Oracle.DatabaseError as e:
        print("Error:Unable to determine the RAW_OBJ_NAME: Object Name:%s, Function Name:%s  Error:%s")%(self.__class__.__name__,sys._getframe().f_code.co_name,
        str(e).strip())
    except cx_Oracle.Warning as e:
        print("Error:Unable to determine the RAW_OBJ_NAME: Object Name:%s, Function Name:%s  Error:%s")%(self.__class__.__name__,sys._getframe().f_code.co_name,
        str(e).strip())                     
    return self.rawname       

Upvotes: 0

Views: 7738

Answers (2)

Anjan Biswas
Anjan Biswas

Reputation: 7912

May be use cx_Oracle._Error.code and if the value is 1403 then its a NO DATA FOUND.

....
except cx_Oracle.DatabaseError as e:
   error, = e.args
   if error.code == 1403:
       print "No rows returned. Error Code:", error.code  
....

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174624

This isn't an error condition, which is probably why none of your exceptions are being triggered. Try something like this instead:

if not self.cursor.rowcount:
   print 'No rows returned'

Upvotes: 2

Related Questions