Rik
Rik

Reputation: 1987

python oracle SQL query conversion

I'm trying to get a certain value from an oracle database and print a string with the value of the oracle database query.

con=cx_Oracle.connect('username/pword')
cur=con.cursor()
fList=()
obj=cur.execute('''select sequence from genes where key=54321''')
cur.close()
con.close()
print str(obj)

I get this output:

<cx_Oracle.Cursor on <cx_Oracle.Connection to user hendrik@local>>

But I'd like to get the value of sequence for this unique key/row.

Upvotes: 1

Views: 948

Answers (1)

Shashank Agarwal
Shashank Agarwal

Reputation: 2804

What you are seeing there is a Cursor object. It in itself is iterable, and it returns a tuple of the selected values; in your case, a tuple of size 1, with the value being the sequence.

Something like this will work -

for values in obj:
    print(values[0])

If you queried for something else, so for example, if your query was select gene_name, sequence from genes where key=54321, then values in the above example will have two items.

If you are absolutely certain that only one result will be returned (or only want the first result), then you can use the fetchone method, and avoid the loop altogether.

Upvotes: 1

Related Questions