Sqlite change Row Value
-
Hi
There is also
https://sqlitebrowser.org/ -
@JonB
Yes, i don't understand why. I do the test and there is no error. I can copy the code and see if can get the error. I don't have Workbench, you say that if i try the statements in the workbench maybe can see the error?Here is the code:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); qDebug()<<"init application..."; QString name; name.append("Datbase1.sqlite"); db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(name); if (!QSqlDatabase::isDriverAvailable("QSQLITE")){ qDebug()<< "Error: QSQLITE is not available"; } if(db.open()) { qDebug()<<"Successfull Conected ."; }else{ qDebug()<<"ERROR! NOT connected to the database."; } CreateStudentTable(); int 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); } ShowData(); }
void MainWindow::insertStudent() { QSqlQuery consult; consult.prepare("INSERT INTO students(" "name," "lastName, " "faults)" "VALUES(" "'"+ui->lineEditName->text()+"'," "'"+ui->lineEditLastName->text()+"'," "'"+ui->lineEditFaults->text()+"'" ");"); if(consult.exec()){ qDebug()<<"The Student is added."; }else{ qDebug()<<"The Student IS NOT added"; qDebug()<<"ERROR! " << consult.lastError(); } }
void MainWindow::deleteStudent() { QString consult; consult.append("DELETE FROM students WHERE name= '"+ui->lineEditName->text()+"'and lastName= '"+ui->lineEditLastName->text()+"'"); QSqlQuery deleteS; deleteS.prepare(consult); if(deleteS.exec()){ qDebug()<<"The student is successfull deleted."; }else{ qDebug()<<"The student IS NOT successfull deleted"; qDebug()<<"ERROR! " << deleteS.lastError(); } }
int MainWindow::ShowData() { QString consult; consult.append("SELECT * FROM students"); QSqlQuery show; show.prepare(consult); if(show.exec()){ qDebug()<<"The STUDENT has consulted correctly."; }else{ qDebug()<<"The STUDENT HAS NOT consulted correctly"; qDebug()<<"ERROR! " << show.lastError(); } int row = 0; ui->tableWidgetData->setRowCount(0); while(show.next()){ ui->tableWidgetData->insertRow(row); ui->tableWidgetData->setItem(row,0,new QTableWidgetItem (show.value(1).toByteArray().constData())); ui->tableWidgetData->setItem(row,1,new QTableWidgetItem (show.value(2).toByteArray().constData())); ui->tableWidgetData->setItem(row,2,new QTableWidgetItem (show.value(3).toByteArray().constData())); row++; } ui->tableWidgetData->horizontalHeader()->setStretchLastSection(true); return row; }
void MainWindow::chequedState(){ int rowCount = ui->listWidget->count(); for (int i = 0; i < rowCount; i++) { if (ui->listWidget->item(i)->checkState() == Qt::Unchecked) { QSqlQuery update; update.prepare("UPDATE students SET faults = faults + 1 WHERE id = :id"); update.bindValue(":id", i); update.exec(); } } }
void MainWindow::on_pushButtonAddStudent_clicked() { insertStudent(); ShowData(); } void MainWindow::on_pushButtonDeleteStudent_clicked() { deleteStudent(); ShowData(); } void MainWindow::on_pushButton_clicked() { chequedState(); ShowData(); }
Maybe the error is in the Buttons or in the list of the check boxes that are separate from the table.
-
If you are saying you are not sure whether your
chequedState()
is even being hit, you should put in a debug statement to verify you are getting there. -
You still have not checked the return result of the
update.prepare("UPDATE students SET faults = faults + 1 WHERE id = :id");
which you say is not working. [EDIT Sorry, I meant theupdate.exec()
which follows that line.] -
You can check your statement by typing
UPDATE students SET faults = faults + 1 WHERE id = 1
into a SQLite command-line/client tool. You can & should get this set up. Once you have done so you can test your proposed SQL statements directly, before you put them into your program. This makes it easier to develop correctly.
-
-
-
If you are saying you are not sure whether your
chequedState()
is even being hit, you should put in a debug statement to verify you are getting there. -
You still have not checked the return result of the
update.prepare("UPDATE students SET faults = faults + 1 WHERE id = :id");
which you say is not working. [EDIT Sorry, I meant theupdate.exec()
which follows that line.] -
You can check your statement by typing
UPDATE students SET faults = faults + 1 WHERE id = 1
into a SQLite command-line/client tool. You can & should get this set up. Once you have done so you can test your proposed SQL statements directly, before you put them into your program. This makes it easier to develop correctly.
-
-
@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
-
-
If you are saying you are not sure whether your
chequedState()
is even being hit, you should put in a debug statement to verify you are getting there. -
You still have not checked the return result of the
update.prepare("UPDATE students SET faults = faults + 1 WHERE id = :id");
which you say is not working. [EDIT Sorry, I meant theupdate.exec()
which follows that line.] -
You can check your statement by typing
UPDATE students SET faults = faults + 1 WHERE id = 1
into a SQLite command-line/client tool. You can & should get this set up. Once you have done so you can test your proposed SQL statements directly, before you put them into your program. This makes it easier to develop correctly.
-
-
@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!
-
@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...
-
@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... -
@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
-
@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.
-
@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.
-
@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
@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 -
@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