1

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.

1
  • obj is a python generator object. You can loop through it (for item in obj:) to retrieve the results of the query. Or it you only want one result: item = obj[0] Commented May 3, 2015 at 6:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Worth mentioning that (1) obj is cur and, as a corollary, (2) you can only fetch data up until the cursor has been closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.