Results 1 to 6 of 6

Thread: Cannot delete an item in MySQL from a model

  1. #1
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Cannot delete an item in MySQL from a model

    I have a qtableview which shows the values of a QSqlModel. Adding an item works fine. When I try to delete an item I get the error "QSqlQuery::value: not positioned on a valid record".

    The itemModel is instantiated as follows:

    Qt Code:
    1. // Setup the Order Items View
    2. itemModel = new QSqlRelationalTableModel(this);
    3. itemModel->setTable("orderitem");
    4. itemModel->setRelation(2, QSqlRelation("products", "ProductID", "ProductName"));
    5. ui->itemsTableView->setModel(itemModel);
    6. ui->itemsTableView->setItemDelegate(new QSqlRelationalDelegate(itemModel));
    7. ui->itemsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    8. ui->itemsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    9. ui->itemsTableView->setColumnHidden(1, true);
    10. ui->itemsTableView->setAlternatingRowColors(true);
    To copy to clipboard, switch view to plain text mode 

    The delete button code is as follows:

    Qt Code:
    1. void addOrder::on_deleteItemPushButton_clicked()
    2. {
    3. // Delete the selected item
    4. int index = ui->itemsTableView->currentIndex().row();
    5. if (!itemModel->removeRow(index))
    6. qDebug() << itemModel->lastError();
    7. ui->savePushButton->setEnabled(true);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas on why the delete does not work?

    I am thinking that maybe the problem lies in the fact that the table in MySQL is actually a combined primary key rather than a single key. It takes the ProductID and the OrderID as a combo primary key. I am considering this problem because on another class which has a single key parameter the same code works fine. Is it possible to send two values to delete a row?

    Thanks,

    Pericles
    Last edited by pcheng; 25th June 2012 at 10:36.

  2. #2
    Join Date
    Mar 2012
    Location
    Lesotho
    Posts
    33
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Cannot delete an item in MySQL from a model

    itemModel in the first function is not the same as the one in the second function, so the second itemModel has no data.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cannot delete an item in MySQL from a model

    What is the value of index in the slot? If there is no current index then this will be -1, an invalid row number. The current index and the selection are not the same thing.

  4. #4
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot delete an item in MySQL from a model

    Kito. The itemModel is the same one since it was declared in the header file as a private. In the add function it works fine so I think that is not the problem. I tried printing an item from the itemModel and it prints out correctly.

    Chris. Index returns the correct index of 1 if I select the second item in the qtableview.

    Qt Code:
    1. void addOrder::on_deleteItemPushButton_clicked()
    2. {
    3. // Delete the selected item
    4. int index = ui->itemsTableView->currentIndex().row();
    5. //QModelIndex index2 = ui->itemsTableView->currentIndex();
    6. qDebug() << "Value of Item at 0,1" << itemModel->data(itemModel->index(0,1), Qt::DisplayRole).toString();
    7. qDebug() << "Value of index" << index;
    8. qDebug() << "Primary Key" << itemModel->primaryKey();
    9.  
    10. if (!itemModel->removeRow(index))
    11. qDebug() << itemModel->lastError();
    12. ui->savePushButton->setEnabled(true);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Value of Item at 0,1 "5"
    2. Value of index 1
    3. Primary Key QSqlRecord( 2 )
    4. " 0:" QSqlField("Orders_OrderID", int, length: 11, precision: 0, required: yes, generated: yes, typeID: 3) ""
    5. " 1:" QSqlField("Products_ProductID", int, length: 11, precision: 0, required: yes, generated: yes, typeID: 3) ""
    6. QSqlQuery::value: not positioned on a valid record
    To copy to clipboard, switch view to plain text mode 

    I have seen some other posts that have a problem with composite keys. Any ideas?


    Added after 1 29 minutes:


    I think I have pinpointed my problem.

    Qt Code:
    1. itemModel->setRelation(2, QSqlRelation("products", "ProductID", "ProductName"));
    To copy to clipboard, switch view to plain text mode 

    I am setting the relationship of the product id with the product name so in the itemTableView I have the names of the products and not the ids. If I remove this line the delete happens correctly. Now to find out how to put the names and then use the name to get the ID to use in the delete function.

    Is there a way to get the actual ProductID instead of the ProductName which substitutes it in the qtableview?

    Thanks,

    Pericles
    Last edited by pcheng; 29th June 2012 at 10:25.

  5. #5
    Join Date
    Mar 2012
    Location
    Lesotho
    Posts
    33
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Cannot delete an item in MySQL from a model

    What happens when you use QSqlQueryModel instead?

  6. #6
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot delete an item in MySQL from a model

    I didn't use QSqlQueryModel but what I did was I created a new primary key and removed the composite primary key from my table. Now the whole thing works fine. The only problem will arise if the user enters the same product in the same order which should not be allowed. Its a small sacrifice that I will need to live with.

    Thanks for your help.

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. Using model indices in complex model item relationships
    By hackerNovitiate in forum Newbie
    Replies: 0
    Last Post: 29th June 2011, 14:30
  3. Replies: 8
    Last Post: 17th June 2011, 21:36
  4. Replies: 0
    Last Post: 14th November 2010, 11:40
  5. MYSQL 5 Table qt model as small Mysql admin
    By patrik08 in forum Qt-based Software
    Replies: 0
    Last Post: 1st May 2007, 09:43

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.