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 the largest primary key in a model?
Forum Updated to NodeBB v4.3 + New Features

How do I find the largest primary key in a model?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 384 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
    #1

    I need to insert a record doing

    model->insertRecord(-1, sqlRecord);
    

    but to insert, I need to specify a new and large enough primary key. How can I obtain the largest primary key in a model??
    Also, after inserting how can I make that row the current in a tableView?

    Regards,
    Juan

    jsulmJ 1 Reply Last reply
    0
    • J jdent

      @jsulm how do I get the value result of the query? I used this:

          QSqlQuery query;
      
          bool ok = query.exec("select max(id) from location");
      

      How do I get the value?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @jdent said in How do I find the largest primary key in a model?:

      How do I get the value?

      By reading documentation: https://doc.qt.io/qt-6/qsqlquery.html#value
      There is also an example:

      QSqlQuery query("SELECT country FROM artist");
      while (query.next()) {
          QString country = query.value(0).toString();
          doSomething(country);
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      4
      • J jdent

        I need to insert a record doing

        model->insertRecord(-1, sqlRecord);
        

        but to insert, I need to specify a new and large enough primary key. How can I obtain the largest primary key in a model??
        Also, after inserting how can I make that row the current in a tableView?

        Regards,
        Juan

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @jdent said in How do I find the largest primary key in a model?:

        I need to specify a new and large enough primary key.

        Why? Primary keys are usually auto-increment and you do not specify them explicitly.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply
        0
        • jsulmJ jsulm

          @jdent said in How do I find the largest primary key in a model?:

          I need to specify a new and large enough primary key.

          Why? Primary keys are usually auto-increment and you do not specify them explicitly.

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

          @jsulm Yet for my case I need to know the largest primary key... Please

          J jsulmJ 2 Replies Last reply
          0
          • J jdent

            @jsulm Yet for my case I need to know the largest primary key... Please

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

            @jdent Also, is there no updateRecord()? if insertRecord is the way how do I find the row number of the model?

            1 Reply Last reply
            0
            • J jdent

              @jsulm Yet for my case I need to know the largest primary key... Please

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @jdent Well, then you will have to query it using max(): https://www.w3schools.com/sql/sql_min_max.asp
              But keep in mind that between querying the max primary key and inserting the record another transaction could increase the primary key. You should do the querying and inserting in one transaction.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply
              2
              • jsulmJ jsulm

                @jdent Well, then you will have to query it using max(): https://www.w3schools.com/sql/sql_min_max.asp
                But keep in mind that between querying the max primary key and inserting the record another transaction could increase the primary key. You should do the querying and inserting in one transaction.

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

                @jsulm how do I get the value result of the query? I used this:

                    QSqlQuery query;
                
                    bool ok = query.exec("select max(id) from location");
                

                How do I get the value?

                jsulmJ 1 Reply Last reply
                0
                • J jdent

                  @jsulm how do I get the value result of the query? I used this:

                      QSqlQuery query;
                  
                      bool ok = query.exec("select max(id) from location");
                  

                  How do I get the value?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @jdent said in How do I find the largest primary key in a model?:

                  How do I get the value?

                  By reading documentation: https://doc.qt.io/qt-6/qsqlquery.html#value
                  There is also an example:

                  QSqlQuery query("SELECT country FROM artist");
                  while (query.next()) {
                      QString country = query.value(0).toString();
                      doSomething(country);
                  }
                  

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply
                  4
                  • jsulmJ jsulm

                    @jdent said in How do I find the largest primary key in a model?:

                    How do I get the value?

                    By reading documentation: https://doc.qt.io/qt-6/qsqlquery.html#value
                    There is also an example:

                    QSqlQuery query("SELECT country FROM artist");
                    while (query.next()) {
                        QString country = query.value(0).toString();
                        doSomething(country);
                    }
                    
                    J Offline
                    J Offline
                    jdent
                    wrote on last edited by
                    #8

                    @jsulm if insertRecord is the way to update a row how do I find the row number of the model? For instance, I just inserted a new record, what is the row number so I can call insertRecord to update the new record?

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

                    • Login

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