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. new a model to a simple table ;
Forum Updated to NodeBB v4.3 + New Features

new a model to a simple table ;

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 412 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    subteam
    wrote on last edited by
    #1

    Re: SQL: How to insert a new record in a QSqlRelationalTableModel

    you should not insert record into a relation table ,but a base table like below:
    int Dialog::addNewAlbum(const QString &title, int artistId)
    {

    QSqlTableModel* themodel = new QSqlTableModel(this);
    themodel->setTable(model->tableName());
    
    themodel->select();
    int id = generateAlbumId();
    QSqlRecord record;
    
    QSqlField f1("albumid", QMetaType(QMetaType::Int));
    QSqlField f2("title", QMetaType(QMetaType::QString));
    QSqlField f3("artistid", QMetaType(QMetaType::Int));
    QSqlField f4("year", QMetaType(QMetaType::Int));
    
    f1.setValue(QVariant(id));
    f2.setValue(QVariant(title));
    f3.setValue(QVariant(artistId));
    f4.setValue(QVariant(yearEditor->value()));
    record.append(f1);
    record.append(f2);
    record.append(f3);
    record.append(f4);
    themodel->insertRecord(-1,record);
    themodel->submitAll();
    model->select();// update the relation table now;
    
    jsulmJ Christian EhrlicherC Pl45m4P 3 Replies Last reply
    0
    • S subteam

      Re: SQL: How to insert a new record in a QSqlRelationalTableModel

      you should not insert record into a relation table ,but a base table like below:
      int Dialog::addNewAlbum(const QString &title, int artistId)
      {

      QSqlTableModel* themodel = new QSqlTableModel(this);
      themodel->setTable(model->tableName());
      
      themodel->select();
      int id = generateAlbumId();
      QSqlRecord record;
      
      QSqlField f1("albumid", QMetaType(QMetaType::Int));
      QSqlField f2("title", QMetaType(QMetaType::QString));
      QSqlField f3("artistid", QMetaType(QMetaType::Int));
      QSqlField f4("year", QMetaType(QMetaType::Int));
      
      f1.setValue(QVariant(id));
      f2.setValue(QVariant(title));
      f3.setValue(QVariant(artistId));
      f4.setValue(QVariant(yearEditor->value()));
      record.append(f1);
      record.append(f2);
      record.append(f3);
      record.append(f4);
      themodel->insertRecord(-1,record);
      themodel->submitAll();
      model->select();// update the relation table now;
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @subteam Why don't you answer in the original thread?

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

      JonBJ 1 Reply Last reply
      0
      • jsulmJ jsulm

        @subteam Why don't you answer in the original thread?

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

        @jsulm Because I believe newcomers to this forum are allowed to post their own topic but not reply in an existing one, for whatever reason, until they get some reputation!?

        Pl45m4P 1 Reply Last reply
        0
        • JonBJ JonB

          @jsulm Because I believe newcomers to this forum are allowed to post their own topic but not reply in an existing one, for whatever reason, until they get some reputation!?

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @JonB

          Also, when you try to reply to an older topic, you get a dialog asking whether you really want to reply directly (and revive the old topic, which sometimes is not a great idea) or if you want to reply in a new topic while referring to the original one.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1
          • S subteam

            Re: SQL: How to insert a new record in a QSqlRelationalTableModel

            you should not insert record into a relation table ,but a base table like below:
            int Dialog::addNewAlbum(const QString &title, int artistId)
            {

            QSqlTableModel* themodel = new QSqlTableModel(this);
            themodel->setTable(model->tableName());
            
            themodel->select();
            int id = generateAlbumId();
            QSqlRecord record;
            
            QSqlField f1("albumid", QMetaType(QMetaType::Int));
            QSqlField f2("title", QMetaType(QMetaType::QString));
            QSqlField f3("artistid", QMetaType(QMetaType::Int));
            QSqlField f4("year", QMetaType(QMetaType::Int));
            
            f1.setValue(QVariant(id));
            f2.setValue(QVariant(title));
            f3.setValue(QVariant(artistId));
            f4.setValue(QVariant(yearEditor->value()));
            record.append(f1);
            record.append(f2);
            record.append(f3);
            record.append(f4);
            themodel->insertRecord(-1,record);
            themodel->submitAll();
            model->select();// update the relation table now;
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @subteam themodel is leaking...

            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
            0
            • S subteam

              Re: SQL: How to insert a new record in a QSqlRelationalTableModel

              you should not insert record into a relation table ,but a base table like below:
              int Dialog::addNewAlbum(const QString &title, int artistId)
              {

              QSqlTableModel* themodel = new QSqlTableModel(this);
              themodel->setTable(model->tableName());
              
              themodel->select();
              int id = generateAlbumId();
              QSqlRecord record;
              
              QSqlField f1("albumid", QMetaType(QMetaType::Int));
              QSqlField f2("title", QMetaType(QMetaType::QString));
              QSqlField f3("artistid", QMetaType(QMetaType::Int));
              QSqlField f4("year", QMetaType(QMetaType::Int));
              
              f1.setValue(QVariant(id));
              f2.setValue(QVariant(title));
              f3.setValue(QVariant(artistId));
              f4.setValue(QVariant(yearEditor->value()));
              record.append(f1);
              record.append(f2);
              record.append(f3);
              record.append(f4);
              themodel->insertRecord(-1,record);
              themodel->submitAll();
              model->select();// update the relation table now;
              
              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              @subteam said in new a model to a simple table ;:

              QSqlTableModel* themodel = new QSqlTableModel(this);

              @Christian-Ehrlicher Isn't themodel integrated in the parent/child hierarchy and therefore deleted with parent (i.e. Dialog class)?!


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              Christian EhrlicherC 1 Reply Last reply
              0
              • Pl45m4P Pl45m4

                @subteam said in new a model to a simple table ;:

                QSqlTableModel* themodel = new QSqlTableModel(this);

                @Christian-Ehrlicher Isn't themodel integrated in the parent/child hierarchy and therefore deleted with parent (i.e. Dialog class)?!

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

                @Pl45m4 said in new a model to a simple table ;:

                integrated in the parent/child hierarchy and therefore deleted with parent (i.e. Dialog class)?!

                Yes, but that's imo much too late here.

                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

                • Login

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