Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python's sqlite3 module, will using cursor.fetchone() as the condition for a while loop fetch a row?

Tags:

python

sqlite

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?

like image 336
Kiteration Avatar asked Jun 06 '26 20:06

Kiteration


1 Answers

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))
like image 158
zero323 Avatar answered Jun 10 '26 18:06

zero323