cx_Oracle : How do I iterate over an outcome set?

There are numerous means to iterate over an outcome set. What are the tradeoff of each?

0
2019-05-07 13:30:46
Source Share
Answers: 1

The approved means is to make use of the constructed - in arrow iterator.

curs.execute('select * from people')
for row in curs:
    print row

You can make use of fetchall() to get all rows simultaneously.

for row in curs.fetchall():
    print row

It can be hassle-free to utilize this to create a Python checklist having the values returned:

curs.execute('select first_name from people')
names = [row[0] for row in curs.fetchall()]

This can be valuable for smaller sized outcome collections, yet can have negative negative effects if the outcome set is huge.

  • You need to await the whole outcome readied to be gone back to your customer procedure.

  • You might consume a great deal of memory in your customer to hold the constructed - up checklist.

  • It might take a while for Python to construct and also deconstruct the checklist which you are mosting likely to quickly throw out anyways.


If you recognize there's a solitary row being returned in the outcome set you can call fetchone() to get the solitary row.

curs.execute('select max(x) from t')
maxValue = curs.fetchone()[0]

Finally, you can loop over the outcome set bring one row at once. As a whole, there's no certain benefit in doing this over making use of the iterator.

row = curs.fetchone()
while row:
    print row
    row = curs.fetchone()
0
2019-05-09 08:11:46
Source