sqlite3 question

Jorgen Bodde jorgen.maillist at gmail.com
Thu Apr 12 03:38:06 EDT 2007


Hi all,

I am using sqlite3 in python, and I wonder if there is a way to know
if there are valid rows returned or not. For example I have a table
song with one entry in it. The ID of that entry is 1, so when I do;

>>> r = c.execute('select * from song where id = 1')
>>> for s in r:
... 	print s
... 	
(1, u'Spikedrivers Blues', u'Mississippi John Hurt')

That works. But when I can't restore the row by e.g. an ID that does
not exist, I cannot see any method in 'r' (which is a SQLite.Cursor)
that can tell me if I have rows. As explained in the help, r.rowcount
does not give a valid result because it can't determine how many rows
are restored in advance.

All I can think of is a 'crappy' construction where I use the iterator
to see if there was something in there, but surely, there must be a
better way to know?

>>> r = c.execute('select * from song where id = 2')
>>> notfound = True
>>> for s in r:
... 	notfound = False
... 	print s
>>> if notfound:
... 	print 'No rows'

I am pretty new with Python, maybe there are some properties of an
iterator / sqlite3 I am not aware of that can tell me how many rows
are there?

With regards,
- Jorgen



More information about the Python-list mailing list