0

As said in cx_oracle documentation, for getting rows of a query i should write a for loop. e.g.:

with connection.cursor() as cursor:
    for row in cursor.execute("select * from MyTable"):
        print(row)

how can I take these rows without for loop? btw, I may use JSON_OBJECT to directly get JSON from oracle. thanks for your attention:)

2
  • 1
    According to PEP-249 the convenient way is to use fetch[one|many|all] method of the cursor object rather than looping over cursor.execute as long as the result of this call is not defined (i.e. implementation-dependent) Commented Apr 20, 2022 at 10:56
  • Are you trying to fetch the rows as a JSON object? Something else? Please clarify! Commented Apr 20, 2022 at 20:56

2 Answers 2

1

You can alternatively use while loop, after fetching all dataset and returning them as may times as the length of the array such as

row = cursor.execute('SELECT * FROM MyTable').fetchall()

i=0
while i < len(row):
    print(row[i])
    i+=1
Sign up to request clarification or add additional context in comments.

Comments

1

The cx_Oracle documentation has some alternatives:

cur = connection.cursor()
cur.execute("select * from MyTable")
while True:
    row = cur.fetchone()
    if row is None:
        break
    print(row)

or

cur = connection.cursor()
cur.execute("select * from MyTable")
num_rows = 10
while True:
    rows = cur.fetchmany(num_rows)
    if not rows:
        break
    for row in rows:
        print(row)

or

cur = connection.cursor()
cur.execute("select * from MyTable")
rows = cur.fetchall()
for row in rows:
    print(row)

Comments

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.