Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I find a record in a QSqlRelationalTableModel if i have the primary key value?
Forum Updated to NodeBB v4.3 + New Features

How do I find a record in a QSqlRelationalTableModel if i have the primary key value?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 253 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jdent
    wrote on last edited by jdent
    #1

    I have this

    	bool ok = model->insertRecord(-1, record);
    	ok = model->submitAll();
    	auto id = model->query().lastInsertId();
    

    ??

    Christian EhrlicherC 1 Reply Last reply
    0
    • J jdent

      I have this

      	bool ok = model->insertRecord(-1, record);
      	ok = model->submitAll();
      	auto id = model->query().lastInsertId();
      

      ??

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Iterate over the model and search for the key.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      J 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Iterate over the model and search for the key.

        J Offline
        J Offline
        jdent
        wrote on last edited by jdent
        #3

        @Christian-Ehrlicher as in this sample?::

        for (int row = 0; row < model->rowCount(); ++row)
        {
        	if (model->record(row).value(0).toInt() == id)
        	{
        		found = true;
        		break;
        	}
        }
        

        is this the best we can do? There is no find(id) method?

        JonBJ 1 Reply Last reply
        0
        • J jdent has marked this topic as solved on
        • J jdent

          @Christian-Ehrlicher as in this sample?::

          for (int row = 0; row < model->rowCount(); ++row)
          {
          	if (model->record(row).value(0).toInt() == id)
          	{
          		found = true;
          		break;
          	}
          }
          

          is this the best we can do? There is no find(id) method?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @jdent
          No, there is no findId() or similar method, else you would see it on https://doc.qt.io/qt-6/qsqlrelationaltablemodel-members.html.

          If you wish to find it efficiently, e.g. if you have a thousand records in memory and don't want to search them sequentially each time, you can add a QSortFilterProxyModel on top of your QSql[Relational]TableModel, or sort the underlying model. Then if you set the sort to be by primary key you know the rows are in ascending primary ley order so you can use a binary search to find a given ID very fast.

          You do not need to use such an extra QSortFilterProxyModel for your view, you could just add it for the purpose of searching. You might get away without bothering to add one. Without a sort the statement will be SELECT * FROM table, with no ORDER BY clauses. Many/most SQL databases will deliver rows in primary key order when no ORDER BY is specified. If your ID column is the primary key they will appear in ID order (and so can be binary searched). However I would regard this as "fragile" as it relies on underlying SQL database behaviour.

          You could also impose a QSortFilterProxyModel and use its setFilter("id = ...") to pick out a given ID. However this does its work by issuing a SELECT ... WHERE id = ... against the table, not be searching rows already read in, and I don't think this is what you are looking for.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved