Results 1 to 9 of 9

Thread: Insert row work only ones

  1. #1
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Insert row work only ones

    I have a QTableView and i am using this code to insert new data.

    Qt Code:
    1. void smith::on_newButton_clicked()
    2. {
    3.  
    4. ui.fEdit->clear();
    5. ui.lnEdit->clear();
    6. ui.cEdit->clear();
    7. ui.cityEdit->clear();
    8.  
    9. ui.newButton->setEnabled(false);
    10. ui.saveEdits->show();
    11. ui.saveNew->show();
    12. ui.cancelButton->show();
    13. ui.nextButton->setEnabled(false);
    14. ui.previousButton->setEnabled(false);
    15. ui.deleteButton->setEnabled(false);
    16. }
    To copy to clipboard, switch view to plain text mode 

    The function clears the forms and takes in new data and upon a click on the save button,the input is passed to saveNew function:

    Qt Code:
    1. void smith::saveNew()
    2. {
    3. //insert code above...
    4. query.bindValue(":firstname", ui.fEdit->text());
    5. query.bindValue(":lastname", ui.lnEdit->text());
    6. query.bindValue(":country", ui.cEdit->text());
    7. query.bindValue(":city", ui.cityEdit->text());
    8. int das = query.lastInsertId().toInt();
    9. query.exec();
    10.  
    11. tableModel->submitAll();
    12. int row = tableModel->rowCount();
    13. tableModel->insertRow(row);
    14. QModelIndex index = tableModel->index(das, firstname);
    15. ui.tableView->setCurrentIndex(index);
    16. ui.tableView->edit(index);
    17.  
    18. ui.newButton->setEnabled(true);
    19. ui.saveNew->setEnabled(true);
    20. ui.saveEdits->setEnabled(true);
    21. ui.nextButton->setEnabled(true);
    22. ui.previousButton->setEnabled(true);
    23. ui.cancelButton->setEnabled(true);
    24. ui.deleteButton->setEnabled(true);
    25.  
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 
    When i click i enter data and click input it works as expected(a new row is inserted,data is inserted to sqlite and the newly created data is presented for editing.However,when i click new button again and click save,no new row is inserted and the lastinsertedrecord shows what i inserted the immediate previous time and not the most new insert but still data is still inserted to sqlite.

    What could be the problem?.
    Last edited by thefatladysingsopera; 8th September 2011 at 08:42.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Insert row work only ones

    It is likely you should use "row" instead of "das" when computing the index to modify the data with. Also if you submit the table, I think you should select() it back again.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    thefatladysingsopera (9th September 2011)

  4. #3
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Insert row work only ones

    I am using "das" to hold the ID of the last inserted record,and surprisingly it works,but only once.I am looking at a past thread of yours http://www.qtcentre.org/threads/2953...rsistent-Index to see if i can learn something from it.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Insert row work only ones

    Quote Originally Posted by thefatladysingsopera View Post
    I am using "das" to hold the ID of the last inserted record,and surprisingly it works
    It will stop working the first time you decide to delete a record from the table.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Insert row work only ones

    Quote Originally Posted by wysota View Post
    It will stop working the first time you decide to delete a record from the table.
    I have not finished writing the delete function but i think it will work because the index work with this time will be:

    Qt Code:
    1. QModelIndex index = ui.tableView->currentIndex();
    To copy to clipboard, switch view to plain text mode 

    but i will try.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Insert row work only ones

    It will not work because if you have 10 rows with id 1-10 and you delete row with id say... "7" then you end up with 9 rows but next time you call lastInsertId() you will get "11" however you will have only 10 rows in the database and your index() call will return an invalid model index.

    Qt Code:
    1. QModelIndex index = tableModel->index(das, firstname); // tableModel->rowCount()==10, das == 11 => index(11, ...) == QModelIndex()
    To copy to clipboard, switch view to plain text mode 

    Besides, if you are using QSqlTableModel then insert rows through that table model and not with an external query, your model doesn't know that you inserted a new row through an external query until you call select() on it again. You have a "nice" synchronization problem here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    thefatladysingsopera (8th September 2011)

  9. #7
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Insert row work only ones

    I got rid of "das" and now have:-

    Qt Code:
    1. tableModel->select();
    2. ..
    3.  
    4. int row = tableModel->rowCount();
    5. tableModel->insertRow(row);
    6. QModelIndex index = tableModel->index(row, firstname);
    7. ui.tableView->setCurrentIndex(index);
    8. ui.tableView->edit(index);
    9. tableModel->submitAll();
    To copy to clipboard, switch view to plain text mode 
    When i run the application, and click save to insert a new row,a blank row is inserted with an asterick(also discussed here: http://developer.qt.nokia.com/forums/viewthread/9064)

    I have run out of ideas on how to move forward.
    Last edited by thefatladysingsopera; 8th September 2011 at 17:16.

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Insert row work only ones

    What code are you calling from the save button?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #9
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Insert row work only ones

    It finally worked after select() again.

    Qt Code:
    1. int row = tableModel->rowCount();
    2. tableModel->insertRow(row);
    3. tableModel->select();
    4. QModelIndex index = tableModel->index(row, firstname);
    5. ui.tableView->setCurrentIndex(index);
    6. ui.tableView->edit(index);
    7. tableModel->submitAll();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 12th July 2011, 08:20
  2. Baisc SQLite insert - does not work
    By johnnyturbo3 in forum Newbie
    Replies: 18
    Last Post: 12th August 2010, 14:15
  3. The way to insert pattern
    By Pablo128 in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2010, 12:37
  4. Replies: 2
    Last Post: 13th December 2009, 20:27
  5. how to insert an ' in a database
    By jh in forum General Programming
    Replies: 3
    Last Post: 17th August 2006, 02:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.