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 can I set the id field of an inserted QSqlRecord?
QtWS25 Last Chance

How can I set the id field of an inserted QSqlRecord?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 488 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 a QSqlRecord with these initializations:

    class PasswordFields
    {
    	QSqlField f1;
    	QSqlField f2;
    	QSqlField f3;
    	QSqlField f4;
    	QSqlRecord rec;
    public:
    	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
    	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
    	{
    		if (id > -1)
    		{
    			f1.setValue(id);
    			rec.append(f1);
    		}
    		f2.setValue(password);
    		f3.setValue(beginDate);
    		f4.setValue(fkLocation);
    		
    		rec.append(f2);
    		rec.append(f3);
    		rec.append(f4);
    	}
    	void setId(int pk)
    	{
    		f1.setValue(pk);
    		rec.append(f1);
    		auto id = rec.value(0).toInt();
    		auto pass = rec.value(1).toString();
    	}
    

    No matter what I do, after I insert the record in the model, there is no way to set the primary key even though I obtain it via

    	auto id = model->query().lastInsertId().toInt();
    

    the pk remains 0 even after calling setId(int pk)!!

    What am I doing wrong?

    J 1 Reply Last reply
    1
    • J jdent

      I have a QSqlRecord with these initializations:

      class PasswordFields
      {
      	QSqlField f1;
      	QSqlField f2;
      	QSqlField f3;
      	QSqlField f4;
      	QSqlRecord rec;
      public:
      	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
      	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
      	{
      		if (id > -1)
      		{
      			f1.setValue(id);
      			rec.append(f1);
      		}
      		f2.setValue(password);
      		f3.setValue(beginDate);
      		f4.setValue(fkLocation);
      		
      		rec.append(f2);
      		rec.append(f3);
      		rec.append(f4);
      	}
      	void setId(int pk)
      	{
      		f1.setValue(pk);
      		rec.append(f1);
      		auto id = rec.value(0).toInt();
      		auto pass = rec.value(1).toString();
      	}
      

      No matter what I do, after I insert the record in the model, there is no way to set the primary key even though I obtain it via

      	auto id = model->query().lastInsertId().toInt();
      

      the pk remains 0 even after calling setId(int pk)!!

      What am I doing wrong?

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

      @jdent It now works:

      class PasswordFields
      {
      	QSqlField f1;
      	QSqlField f2;
      	QSqlField f3;
      	QSqlField f4;
      	QSqlRecord rec;
      public:
      	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
      	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
      	{
      		if (id > -1)
      		{
      			f1.setValue(id);
      			rec.append(f1);
      		}
      		f2.setValue(password);
      		f3.setValue(beginDate);
      		f4.setValue(fkLocation);
      		
      		rec.append(f2);
      		rec.append(f3);
      		rec.append(f4);
      	}
      	void setId(int pk)
      	{
      		f1.setValue(pk);
      		rec.insert(0, f1);
      	}
      

      The change was in rec.insert(0,f1) instead of rec.append(f1)... why???

      JonBJ Christian EhrlicherC 2 Replies Last reply
      0
      • JonBJ JonB referenced this topic on
      • J jdent

        @jdent It now works:

        class PasswordFields
        {
        	QSqlField f1;
        	QSqlField f2;
        	QSqlField f3;
        	QSqlField f4;
        	QSqlRecord rec;
        public:
        	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
        	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
        	{
        		if (id > -1)
        		{
        			f1.setValue(id);
        			rec.append(f1);
        		}
        		f2.setValue(password);
        		f3.setValue(beginDate);
        		f4.setValue(fkLocation);
        		
        		rec.append(f2);
        		rec.append(f3);
        		rec.append(f4);
        	}
        	void setId(int pk)
        	{
        		f1.setValue(pk);
        		rec.insert(0, f1);
        	}
        

        The change was in rec.insert(0,f1) instead of rec.append(f1)... why???

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

        @jdent
        No cross-reference from the OP, but see also https://forum.qt.io/topic/155730/qsqltablemodel-insertrecord-row-record-always-appends-the-new-record-never-uses-the-row-parameter/9.

        J 1 Reply Last reply
        1
        • JonBJ JonB

          @jdent
          No cross-reference from the OP, but see also https://forum.qt.io/topic/155730/qsqltablemodel-insertrecord-row-record-always-appends-the-new-record-never-uses-the-row-parameter/9.

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

          @JonB This issue has nothing related to my other issue!

          1 Reply Last reply
          0
          • J jdent

            @jdent It now works:

            class PasswordFields
            {
            	QSqlField f1;
            	QSqlField f2;
            	QSqlField f3;
            	QSqlField f4;
            	QSqlRecord rec;
            public:
            	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
            	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
            	{
            		if (id > -1)
            		{
            			f1.setValue(id);
            			rec.append(f1);
            		}
            		f2.setValue(password);
            		f3.setValue(beginDate);
            		f4.setValue(fkLocation);
            		
            		rec.append(f2);
            		rec.append(f3);
            		rec.append(f4);
            	}
            	void setId(int pk)
            	{
            		f1.setValue(pk);
            		rec.insert(0, f1);
            	}
            

            The change was in rec.insert(0,f1) instead of rec.append(f1)... why???

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @jdent said in How can I set the id field of an inserted QSqlRecord?:

            The change was in rec.insert(0,f1) instead of rec.append(f1)... why???

            Because insert does insert it at the specified position and append adds it to the end. Don't know how more obvious to make the function names...

            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

              @jdent said in How can I set the id field of an inserted QSqlRecord?:

              The change was in rec.insert(0,f1) instead of rec.append(f1)... why???

              Because insert does insert it at the specified position and append adds it to the end. Don't know how more obvious to make the function names...

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

              @Christian-Ehrlicher you misunderstand... of course I understand the difference between insert and append! That's not the point.

              What I don't understand is why programming this method works differently:

              void setId(int pk)
              {
              f1.setValue(pk);
              rec.insert(0, f1); // THIS updates the primary key of the object in memory
              }

              void setId(int pk)
              {
              f1.setValue(pk);
              rec.append(f1); // THIS DOES NOT update the primary key of the object in memory
              }

              Why??

              Christian EhrlicherC 1 Reply Last reply
              0
              • J jdent

                @Christian-Ehrlicher you misunderstand... of course I understand the difference between insert and append! That's not the point.

                What I don't understand is why programming this method works differently:

                void setId(int pk)
                {
                f1.setValue(pk);
                rec.insert(0, f1); // THIS updates the primary key of the object in memory
                }

                void setId(int pk)
                {
                f1.setValue(pk);
                rec.append(f1); // THIS DOES NOT update the primary key of the object in memory
                }

                Why??

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @jdent said in How can I set the id field of an inserted QSqlRecord?:

                Why??

                Because, as the function names tell you, f1 is appended to your QSqlRecord (which only contains values).

                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

                  @jdent said in How can I set the id field of an inserted QSqlRecord?:

                  Why??

                  Because, as the function names tell you, f1 is appended to your QSqlRecord (which only contains values).

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

                  @Christian-Ehrlicher Again my point is not being understood. See this code and maybe it can become clear to all finally:

                  class PasswordFields
                  {
                  	QSqlField f1;
                  	QSqlField f2;
                  	QSqlField f3;
                  	QSqlField f4;
                  	QSqlRecord rec;
                  public:
                  	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
                  	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
                  	{
                  		if (id > -1)
                  		{
                  			f1.setValue(id);
                  			rec.append(f1);
                  		}
                  		f2.setValue(password);
                  		f3.setValue(beginDate);
                  		f4.setValue(fkLocation);
                  		
                  		rec.append(f2);
                  		rec.append(f3);
                  		rec.append(f4);
                  	}
                  	void setId(int pk)
                  	{
                  		f1.setValue(pk);
                  		rec.insert(0, f1);
                  	}
                  

                  When I call PasswordFields with id > -1, then f1 is set in QSqlRecord by means of a

                  f1.setValue(id);
                  rec.append(f1);
                  

                  otherwise f1 remains unset. When after insertion of the record I finally obtain the QSqlRecord's primary key, I call pwd.setId(5);

                  which does nothing to update the f1 field in the record if we use

                  f1.setValue(id);
                  rec.append(f1);
                  

                  On the other hand, if setId() is

                  f1.setValue(id);
                  rec.insert(0,f1);
                  

                  then the f1 field is successfully updated in the record in memory!

                  See the difference? Why?

                  Christian EhrlicherC JonBJ 2 Replies Last reply
                  0
                  • J jdent

                    @Christian-Ehrlicher Again my point is not being understood. See this code and maybe it can become clear to all finally:

                    class PasswordFields
                    {
                    	QSqlField f1;
                    	QSqlField f2;
                    	QSqlField f3;
                    	QSqlField f4;
                    	QSqlRecord rec;
                    public:
                    	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
                    	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
                    	{
                    		if (id > -1)
                    		{
                    			f1.setValue(id);
                    			rec.append(f1);
                    		}
                    		f2.setValue(password);
                    		f3.setValue(beginDate);
                    		f4.setValue(fkLocation);
                    		
                    		rec.append(f2);
                    		rec.append(f3);
                    		rec.append(f4);
                    	}
                    	void setId(int pk)
                    	{
                    		f1.setValue(pk);
                    		rec.insert(0, f1);
                    	}
                    

                    When I call PasswordFields with id > -1, then f1 is set in QSqlRecord by means of a

                    f1.setValue(id);
                    rec.append(f1);
                    

                    otherwise f1 remains unset. When after insertion of the record I finally obtain the QSqlRecord's primary key, I call pwd.setId(5);

                    which does nothing to update the f1 field in the record if we use

                    f1.setValue(id);
                    rec.append(f1);
                    

                    On the other hand, if setId() is

                    f1.setValue(id);
                    rec.insert(0,f1);
                    

                    then the f1 field is successfully updated in the record in memory!

                    See the difference? Why?

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Again - your record is ordered. When you access the record with QSqlRecord::value(int) you will get a different value depending on appending or inserting. But you don't show the code you're using the created record. Also I don't understand why you store your data twice - once in separate QSqlFields and once in QSqlRecord.

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

                    1 Reply Last reply
                    1
                    • J jdent

                      @Christian-Ehrlicher Again my point is not being understood. See this code and maybe it can become clear to all finally:

                      class PasswordFields
                      {
                      	QSqlField f1;
                      	QSqlField f2;
                      	QSqlField f3;
                      	QSqlField f4;
                      	QSqlRecord rec;
                      public:
                      	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
                      	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
                      	{
                      		if (id > -1)
                      		{
                      			f1.setValue(id);
                      			rec.append(f1);
                      		}
                      		f2.setValue(password);
                      		f3.setValue(beginDate);
                      		f4.setValue(fkLocation);
                      		
                      		rec.append(f2);
                      		rec.append(f3);
                      		rec.append(f4);
                      	}
                      	void setId(int pk)
                      	{
                      		f1.setValue(pk);
                      		rec.insert(0, f1);
                      	}
                      

                      When I call PasswordFields with id > -1, then f1 is set in QSqlRecord by means of a

                      f1.setValue(id);
                      rec.append(f1);
                      

                      otherwise f1 remains unset. When after insertion of the record I finally obtain the QSqlRecord's primary key, I call pwd.setId(5);

                      which does nothing to update the f1 field in the record if we use

                      f1.setValue(id);
                      rec.append(f1);
                      

                      On the other hand, if setId() is

                      f1.setValue(id);
                      rec.insert(0,f1);
                      

                      then the f1 field is successfully updated in the record in memory!

                      See the difference? Why?

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

                      @jdent
                      Please append a qDebug() statement to each of your append/insert code fragments showing what you are comparing which you say changes/does not change.

                      1 Reply Last reply
                      0

                      • Login

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