I am trying to use SQLite in a PyQt4 application. I am running into a problem where it is taking a really long time to get the data out of the QSqlQuery object after a query is completed. Here is a code snippet with the relevant parts:
Qt Code:
  1. # Begin Snippet
  2. self.selected_columns = self.table_definition['track']
  3. select_string = ','.join(self.selected_columns)
  4.  
  5. query_str = 'SELECT %s FROM track %s'%(select_string, where_string)
  6.  
  7. start_time = time.clock()
  8. self.query.prepare(query_str)
  9. self.query.exec_()
  10. util.message('Query took:', time.clock() - start_time, 'seconds')
  11.  
  12. print ''
  13. print query_str
  14.  
  15. results = []
  16. header = self.selected_columns
  17.  
  18. self.query.first()
  19.  
  20. while self.query.isValid():
  21. record = [self.query.value(index).toString() for index in range(len(header))]
  22. results.append(record)
  23. self.query.next()
  24.  
  25. util.message('Selected', len(results), 'records.')
  26.  
  27. util.message('Unpacking Took:', time.clock() - start_time, 'seconds')
To copy to clipboard, switch view to plain text mode 

While the query is quick, extracting the data takes a really long time. Way longer than I think it should take.

Here are the results:

Qt Code:
  1. Query took: 0.00094797466213 seconds
  2.  
  3. SELECT artist,album,title,url,musicbrainz_trackid,hystrix_trackid FROM track
  4. Selected 16668 records.
  5. Unpacking Took: 5.29902267251 seconds
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. Query took: 0.0317866355896 seconds
  2.  
  3. SELECT artist,album,title,url,musicbrainz_trackid,hystrix_trackid FROM track WHERE (album LIKE '%it%' OR title LIKE '%it%' OR artist LIKE '%it%')
  4. Selected 3262 records.
  5. Unpacking Took: 1.4495204451 seconds
To copy to clipboard, switch view to plain text mode 

I have tried using setForwardOnly() but it made no difference.

Does anyone have any idea on why this is taking so long or how I can improve things?

-- amicitas

Hystrix Audio