Unsolved Sqlite change Row Value
-
@JonB
Yes i did that and dont throw a error and i delete it from the code. I will try to do that in the page of sqlite -
@VRonin @JonB
Yes, here i add the checkboxesint row = ShowData(); setWindowTitle(QString ("Assistance")); QList<QString> items; for (int i = 0; i<row;i++) { items.append("" + QString::number(i)); } QListIterator <QString> itr(items); while(itr.hasNext()) { QListWidgetItem *item = new QListWidgetItem(itr.next()); item->setCheckState(Qt::Unchecked); ui->listWidget->addItem(item); }
Maybe the error ocurred here in the creation of the checkboxes and i need to do it in other function and other way
-
@JonB I test it and if i do this:
UPDATE students SET faults = faults + 1 WHERE id = id
then increment by 3 all the faults and if i uncheck one, increment all by 1. So now the thing is in the for loop. im trying to change it
-
@JonB @jsulm
Can anyone help me with this last thing?void MainWindow::chequedState(int i){ if (ui->listWidget->item(i)->checkState() == Qt::Unchecked) { QSqlQuery update; update.bindValue(":id", i); update.prepare("UPDATE students SET faults = faults + 1 WHERE id"); update.exec(); } } /void MainWindow::saveAssistence() { int rowCount = ui->listWidget->count(); for (int i = 0; i < rowCount; i++) { chequedState(i); } }
I have this 2 functions and increments in 3 instead in 1 each row.
Thank you!
-
@Aioria said in Sqlite change Row Value:
UPDATE students SET faults = faults + 1 WHERE id
please take a closer look at the WHERE part of your update query...
-
@jsulm
I tried to put =: id but don't do nothing. I think that the problem is in the for. Maybe another way to do it. This increment all the rows in the quantity of rows that the table have... -
@Aioria Are you sure the row number in the list widget is the correct ID in the table?!
-
@jsulm I think with the :id, i , i put the value of i in the id.
How can i check that and how can i do to make an union between the list and the table?CreateStudentTable(); int row = ShowData(); setWindowTitle(QString ("Asistencia")); QList<QString> items; for (int i = 0; i<row;i++) { items.append("" + QString::number(i)); } QListIterator <QString> itr(items); while(itr.hasNext()) { QListWidgetItem *item = new QListWidgetItem(itr.next()); item->setCheckState(Qt::Unchecked); ui->listWidget->addItem(item); } ShowData();
This is the code that creates the list with the checkbox
-
-
@Pablo-J-Rogina it's usually the only case where double posting is allowed since it's in the poster native language and therefor might get an answer that's easier to understand.
-
@SGaist got it, it's learnt since now on forward. Thank you for the clarification.
-
@Aioria said in Sqlite change Row Value:
I think with the :id, i , i put the value of i in the id
Yes, you do and this is a bad idea. The id of an item in the database should not be its row number in some widget in your UI! What happens, for example, if you order/sort the items differently? Then suddenly all your IDs are wrong.
You can use http://doc.qt.io/qt-5/qlistwidgetitem.html#setData to set the database ID in your items and then get it using http://doc.qt.io/qt-5/qlistwidgetitem.html#data -
@jsulm
Thank you! I will try to do that instead the bindvalue!