Home > Mobile >  how can i get rows from cx_oracle without for loop
how can i get rows from cx_oracle without for loop

Time:04-21

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:)

CodePudding user response:

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

CodePudding user response:

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)
  • Related