I am using python's sqlite3 module.
I want to iterate through the rows in my table until none are left. I want to do this one at a time instead of using fetchall because the table is quite large. I read in the python sqlite3 documentation that curs.fetchone() returns None when no rows are left in the cursor's selection.
So I had the idea to run my code in a while fetchone(): loop, with the 'actual' fetchone() call occuring inside the loop, like so:
while c.fetchone():
row = c.fetchone()
print(foo(row))
I can't tell if this loop is actually fetching a row once in the while statement and once in the body, or if it is only in the body. Which one is it?
It will execute fetchone twice so it is not a good idea. Since sqlite3.Cursor is iterable you do something like this:
for row in c:
print(foo(row))
Alternatively you can use infinite while loop and fetch data in batches using fetchmany:
# Lets take 1000 rows at the time
batch_size = 1000
while True:
rows = c.fetchmany(batch_size)
if not rows: break
for row in rows:
print(foo(row))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With