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():
if query.next():
To copy to clipboard, switch view to plain text mode
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():
while query.next():
To copy to clipboard, switch view to plain text mode
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.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
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
To copy to clipboard, switch view to plain text mode
- amicitas
Bookmarks