Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Solved]Add new row at last in SQLite

[Solved]Add new row at last in SQLite

Scheduled Pinned Locked Moved Mobile and Embedded
10 Posts 2 Posters 2.7k 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.
  • Awadhesh MauryaA Offline
    Awadhesh MauryaA Offline
    Awadhesh Maurya
    wrote on last edited by Awadhesh Maurya
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • Awadhesh MauryaA Offline
        Awadhesh MauryaA Offline
        Awadhesh Maurya
        wrote on last edited by
        #3

        @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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • Awadhesh MauryaA Offline
            Awadhesh MauryaA Offline
            Awadhesh Maurya
            wrote on last edited by Awadhesh Maurya
            #5

            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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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
              0
              • Awadhesh MauryaA Offline
                Awadhesh MauryaA Offline
                Awadhesh Maurya
                wrote on last edited by Awadhesh Maurya
                #7

                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
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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
                  0
                  • Awadhesh MauryaA Offline
                    Awadhesh MauryaA Offline
                    Awadhesh Maurya
                    wrote on last edited by
                    #9

                    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
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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
                      0

                      • Login

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