Oddbean new post about | logout
 TIL python sqlite3 cursor objects are iterable, was always calling fetchone one by one 😭 (or fetchmany) 
 so I did always. tnx for the hint lol. 
 Does it fetchone for every iteration? 
 i think so (haven't checked the source, though) 
 C implementation:
- cursor_iternext: https://github.com/python/cpython/blob/main/Modules/_sqlite/cursor.c#L1090
- cursor_fetchone_impl: https://github.com/python/cpython/blob/main/Modules/_sqlite/cursor.c#L1142

so yes, fetchone literally just calls next() on the iterator 🤷‍♀️ 

why did they even bother to add it ?!? i don't know, maybe it wasn't always this way, eg from what i remember iterators were much less common in python 2.x times 
 Name kinda gives it away though. 
 And then you created a loop yourself with a conditional in it for when you reached the last object? 

Never used sqlite3 much with python, only to get some stats from my nostr-rs-relay. 
 Ah did not read the fetchmany somehow 🤦‍♂️ 
 i use sqlite3 a lot, it's great as application database--but in this case it's also for prying into nostr-rs-relay's database

yes, just fetchone in a loop usually

while True
    rec = cursor.fetchone()
    if rec is None: 
        break

which works the same i guess but it's more typing

if you're concerned about performance you'd also do this, but fetching a batch at the same time with fetchmany

there's also fetchall when you want to fetch everything, but don't expect so many it'd exhaust memory