PDA

View Full Version : SQLite - check if any records are returned



amicitas
28th September 2008, 00:43
I am trying to learn how to use SQLite with PyQt4. (And I am fairly new to both.)

My question is how do I check if any results were returned from a SELECT query?
At this point the only way I know to do this is to use

if query.next():
This however increments the query position. Now if I want to loop over the records in the query I can't use a standard loop unless unless I seek back to before the first record.

while query.next():

My second question is whether or not there will be a performance improvement using setForwardOnly (setForwardMode) with the SQLite driver. I will eventually have a database with ~10,000 entries and I will be doing repeated searches, so speed may be an issue. If there will be no improvement then using query.seek(-1) would work for me.

Here a snippet of what I want to do:


self.database = QtSql.QSqlDatabase.addDatabase("QSQLITE", "main")
self.database.setDatabaseName(database_name)
status = self.database.open()

self.query = QtSql.QSqlQuery(self.database)

self.query.prepare("SELECT id FROM track WHERE id=:id")
self.query.bindValue(":id", QtCore.QVariant(id))
self.query.exec_()

if self.query.next():
self.query.seek(-1)
# This next loop would be in an underlying function.
while self.query.next():
# Do some stuff.
pass



- amicitas

janus
28th September 2008, 10:28
My question is how do I check if any results were returned from a SELECT query?


Hi, as far as I know sqlite does not the support something like query.size(). I am doing something like this to check if there are any records:


QSqlQuery q("SELECT * FROM MyTable");
while(q.next())
if (!q.value(0).isValid())
dosomething();