Results 1 to 2 of 2

Thread: SQLite - check if any records are returned

  1. #1
    Join Date
    Sep 2008
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question SQLite - check if any records are returned

    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
    Qt Code:
    1. 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.
    Qt Code:
    1. 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:
    Qt Code:
    1. self.database = QtSql.QSqlDatabase.addDatabase("QSQLITE", "main")
    2. self.database.setDatabaseName(database_name)
    3. status = self.database.open()
    4.  
    5. self.query = QtSql.QSqlQuery(self.database)
    6.  
    7. self.query.prepare("SELECT id FROM track WHERE id=:id")
    8. self.query.bindValue(":id", QtCore.QVariant(id))
    9. self.query.exec_()
    10.  
    11. if self.query.next():
    12. self.query.seek(-1)
    13. # This next loop would be in an underlying function.
    14. while self.query.next():
    15. # Do some stuff.
    16. pass
    To copy to clipboard, switch view to plain text mode 

    - amicitas

  2. #2
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: SQLite - check if any records are returned

    Quote Originally Posted by amicitas View Post
    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:

    Qt Code:
    1. QSqlQuery q("SELECT * FROM MyTable");
    2. while(q.next())
    3. if (!q.value(0).isValid())
    4. dosomething();
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.