1

I use SQLite 3.x, Qt 5.7, C++, QSqlTableModel, QTableView classes. The table sites contains unique url column. I'd like to avoid redundant selects and minimize/exclude full resets of my model in the case of small data modifications. So, I prefer using read-only raw SQL queries and writing via QSqlRecord in this project. I update the row in the model through changing the corresponding QSqlRecord instance. But I have to make linear search of the record to update (see pseudocode below):

for (int i = 0; i < rowCount(); ++i)
{
    QSqlRecord current_record = record(i);
    QString current_url = current_record.value("url").toString();
    if (current_url == url)
    {
       //updates... 
       break;
    }
} 

I'd like to use SQLite internal means for quicker search. How can I find correct identifier (let's name it found_id) in SQLite in order to call QSqlRecord record = row(found_id);? Note that inserts and removals will be used too.

Here is simplified DDL:

CREATE TABLE sites (
id                 INTEGER PRIMARY KEY,
url                VARCHAR UNIQUE NOT NULL
                         );
ilya
  • 1,103
  • 14
  • 36
  • 1
    There are many variants, For example, using mapping variable like `QHash mapUrlToRecord`. After initialize it, we can quickly access to record from its url by `mapUrlToRecord.value(url)`. On updates we changing this variable too. If we want access to records from id, we just use integer as a key: `QHash mapIdToRecord` – Rinat Nov 03 '16 at 10:28
  • Anyway, I think that subclassing from `QAbstractTableModel` with overriding `rowCount`, `columnCount`, `data`, `setData` methods is more "true-way". – Rinat Nov 03 '16 at 10:33

0 Answers0