Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    [Solved]Add new row at last in SQLite

    Mobile and Embedded
    2
    10
    2192
    Loading More Posts
    • 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.
    • Awadhesh Maurya
      Awadhesh Maurya last edited by Awadhesh Maurya

      Hi,
      I want add new row after last row in QTableView, my code is:

      void AbstractItemModel :: UpdateData(QModelIndex start, QModelIndex end)
      {
      bool ret;
      int columns = columnCount();
      int row = rowCount()+1;
      beginInsertRows(end, row, row);

      ret = setData(index(row, 0), gID, Qt::EditRole);
      fprintf(stdout,"\rsetData gID %d\n", ret);
      ret = setData(index(row, 1), gTitle);
      fprintf(stdout,"\rsetData gTitle %d\n", ret);
      setData(index(row, 2), gFName);
      setData(index(row, 3), gMName);
      setData(index(row, 4), gLName);
      setData(index(row, 7), gDob);
      
      setData(index(row, 5), gUsrTp);
      setData(index(row, 6), gGend);
      setData(index(row, 8), gGroup);
      setData(index(row, 9), gGrade);
      setData(index(row, 10), gEmpTp);
      setData(index(row, 11), gShift);
      
      endInsertRows();
      emit dataChanged(end, end);
      

      }

      In this way there is only blank row added in table, but actual rowCount() is not changed. setData always return 0, i.e. its not setting data.
      I think I am missing something, please let me know where I am wrong.

      Thanks.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Maybe a silly question, but since you are using SQLite, why don't use use a QSqlTableModel ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • Awadhesh Maurya
          Awadhesh Maurya last edited by

          @SGaist said:

          QSqlTableModel

          hi,
          I thought Qt using SQLite so I have written these. I am using QSqlTableModel. I don't have enough knowledge about SQL. After searching in google I found that I should use QAbstractItemModel to modify the table. I set

          aim = (AbstractItemModel*)model;

          where is aim - pointer AbstractItemModel derived class from QAbstractItemModel;
          model is QSqlTableModel pointer type.

          I want to add row in table, so I am using QAbstractItemModel. What are the issue why my program is not working.

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            You absolutely don't need all of that, QSqlTableModel provides all what you need.

            One way to do it:

            QSqlRecord record = model->record();
            record.setValue(0, gID);
            record.setValue(1, gTitle);
            record.setValue(2, gFName);
            //etc.
            model->insertRecord(-1, record);
            

            You should also take a look at the QDataWidgetMapper class

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • Awadhesh Maurya
              Awadhesh Maurya last edited by Awadhesh Maurya

              hi,

              thanks for your reply.
              I have tried this also but in table row number is star symbol and after any activity like sorting, this row will be disappeared from table, i.e. I am getting new data in new row nunbered as '*'.

              I am using QtableView with setSortingEnabled.

              Please can you suggest me to solve it.

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                @Awadhesh-Maurya said:

                I have tried this also but in table row number is star symbol and after any activity like sorting, this row will be disappeared from table, i.e. I am getting new data in new row nunbered as '*'.

                I don't understand the "*" can you show maybe a picture of what you are getting ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • Awadhesh Maurya
                  Awadhesh Maurya last edited by Awadhesh Maurya

                  Previous row like:

                  15							96765432					Mr.					Awadhesh								skumar         
                  

                  New inserted row:

                  *							98765432					Mr.					XYZ									ABC
                  

                  Before insert new at end there were 15 rows numbered as 1, 2,3 ....15. Newly added row should be 16 but here it is *. After sorting column this row will be removed automatically.

                  I think now you can understand what am I trying to say, because there is no option to attach file, so I am not able send image of table.

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    Are you using the OnManualSubmit strategy ? If so did you submit the data ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply Reply Quote 0
                    • Awadhesh Maurya
                      Awadhesh Maurya last edited by

                      Thank you for your help.

                      Now it is solved. I am using the OnManualSubmit strategy. After
                      model->insertRecord(-1, record);

                      I do submit
                      model->submitAll();

                      Now its working properly.

                      1 Reply Last reply Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        Good !

                        Since you have it running properly, please mark the thread as solved so other forum users may know a solution has been found :)

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post