>Possibly the Python documentation is overwhelming here as data can also be
>retrieved with explicit fetch steps.

The APSW documentation is better.  The sqlite3 documentation is somewhat 
limited.

>>> import sqlite3
>>> db = sqlite3.connect('', isolation_level=None) # isolation_level=None turns 
>>> off automagic which does not work anyway
>>> db.execute('create table x(x)')
<sqlite3.Cursor object at 0x000001BE183BEEA0>

.fetchone() returns None if there is no rows, else it returns the row tuple:

>>> print(db.execute('select x from x').fetchone())
None

.fetchall() returns an list of rows (which will be empty if there are no rows):

>>> print(db.execute('select x from x').fetchall())
[]

Calling the iterator step function manually will throw StopIteration if there 
are no more rows (that is how "for row in iter(...)" knows to stop iterating -- 
the for catches the StopIteration exception and stops executing the loop):

>>> print(next(db.execute('select x from x')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

>>> print(db.execute('select x from x').__next__())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration


APSW works the same way.  https://github.com/rogerbinns/apsw

>>> import apsw
>>> db = apsw.Connection('')
>>> db.cursor().execute('create table x(x)')
<apsw.Cursor object at 0x000001BE17D36C30>

>>> print(db.cursor().execute('select x from x').fetchone())
None

>>> print(db.cursor().execute('select x from x').fetchall())
[]

>>> print(next(db.cursor().execute('select x from x')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

>>> print(db.cursor().execute('select x from x').__next__())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

--  
The fact that there's a Highway to Hell but only a Stairway to Heaven says a 
lot about anticipated traffic volume.



_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to