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. Cannot insert new record when database is empty
Forum Updated to NodeBB v4.3 + New Features

Cannot insert new record when database is empty

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 717 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.
  • P Offline
    P Offline
    Panoss
    wrote on last edited by Panoss
    #1

    I 'm working with SQLite database.
    When the database has at least one record, I can insert new record normally.
    But when it has no record, I can't insert a new record.

    The code of the new record button is this:

    void RepairDevices::on_newButton_clicked()
    {     
        isnewRecord = true;
        if (!model->isDirty()){
    	int row = mapper->currentIndex();
            mapper->submit();
            model->insertRow(row);
            mapper->setCurrentIndex(row);
    
            // clear controls
            ui.id_txt->clear();
            ui.type_cbo->clear();        
            ui.sn_txt->clear();
            ui.model_txt->clear();        
            ui.client_id_cbo->clear();
    
    	// set focus on first control
            ui.type_cbo->setFocus();
    }
    

    The code of the save button:

    void RepairDevices::on_saveButton_clicked()
    {
        model->submitAll();
        if(isnewRecord){
            mapper->toLast();
            isnewRecord = false;
            int row = mapper->currentIndex();
            updateButtons(row);
        }else{
            // do nothing
        }
    }
    

    Model is a QSqlRelationalTableModel.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Where is the code to insert data into db ? I suspect some logic issue in your program

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      4
      • P Offline
        P Offline
        Panoss
        wrote on last edited by Panoss
        #3

        The code that inserts data into db is this, in the save button:

            model->submitAll();
        

        Any suggestion for improving my code, is welcome.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Your index management looks off by one. if your model is empty and you create a new row, it's index will be 0.

          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
          -1
          • P Offline
            P Offline
            Panoss
            wrote on last edited by Panoss
            #5

            Hi SGaist.
            So, how can I fix this?

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Panoss
              wrote on last edited by
              #6

              At last I found it!
              I had it like this:

              int row = mapper->currentIndex();
              

              Replaced with:

              int row = model->rowCount(); 
              

              and works fine!!

              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