Mani kB
Mani kB

Reputation: 43

Python With Oracle Database

I am on Oracle-12c with Python-3 and cx-oracle.

How to handle the below query of "No rows returned" for the SQL? If no rows returned I would like the string "Inactive" assigned to the result.

cursor.execute("select STATUS from DOMAINTABLE")
for result in cursor:
    print(result[0])
    row = result.fetchone()
    if row == None:
        break
        print ("Inactive")

Upvotes: 1

Views: 59

Answers (1)

blhsing
blhsing

Reputation: 106543

You should either iterate over the cursor or call the fetchone method but not do both:

cursor.execute("select STATUS from DOMAINTABLE")
for status, in cursor:
    print(status)
    break
else:
    print('Inactive')

or:

cursor.execute("select STATUS from DOMAINTABLE")
row = result.fetchone()
if row is None:
    print('Inactive')
else:
    print(row[0])

Upvotes: 1

Related Questions