Sqlite change Row Value
-
Hi! im trying to do an app like Teacher`s assistence and i have one problem when i try to save the state of a checkBox. i need to increment in one the value of the faults but i cant do that.
Here is my code:void MainWindow::saveAssistence() { QString consult; consult.append("UPDATE student SET faults = 2 WHERE id =1"); QSqlQuery update; update.prepare(consult); if(update.exec()){ qDebug()<<"the student is successful update."; }else{ qDebug()<<"the student is NOT successful update."; qDebug()<<"ERROR! " << update.lastError(); } }
and when i check if the checkBox is unchecked to increment the value of "Faults" i can't do that because i change all over the Column of "Faults"
void MainWindow::chequedState(){ int rowCount = ui->listWidget->count(); for (int i=0; i<rowCount; i++) { if(ui->listWidget->item(i)->checkState() == Qt::Unchecked) saveAssistence(); } }
void MainWindow::on_pushButton_clicked() { chequedState(); ShowData(); }
I don't know how to do the consult so that only cell "Faults" increment the value in just one.
Thank you!! -
Hi! im trying to do an app like Teacher`s assistence and i have one problem when i try to save the state of a checkBox. i need to increment in one the value of the faults but i cant do that.
Here is my code:void MainWindow::saveAssistence() { QString consult; consult.append("UPDATE student SET faults = 2 WHERE id =1"); QSqlQuery update; update.prepare(consult); if(update.exec()){ qDebug()<<"the student is successful update."; }else{ qDebug()<<"the student is NOT successful update."; qDebug()<<"ERROR! " << update.lastError(); } }
and when i check if the checkBox is unchecked to increment the value of "Faults" i can't do that because i change all over the Column of "Faults"
void MainWindow::chequedState(){ int rowCount = ui->listWidget->count(); for (int i=0; i<rowCount; i++) { if(ui->listWidget->item(i)->checkState() == Qt::Unchecked) saveAssistence(); } }
void MainWindow::on_pushButton_clicked() { chequedState(); ShowData(); }
I don't know how to do the consult so that only cell "Faults" increment the value in just one.
Thank you!!-
. Make
saveAssistence()
accept afaults
parameter, which it inserts into the"UPDATE student SET faults = 2 WHERE id =1";
line in place of the2
you have (or do it via a SQL parameter). -
In function
chequedState()
, do not callsaveAssistence()
as you iterate through each checkbox. Instead, increment afaults
variable, so that at the end of the loop it holds the total number of faults. Then callsaveAssistence(faults)
once at the end.
-
-
-
. Make
saveAssistence()
accept afaults
parameter, which it inserts into the"UPDATE student SET faults = 2 WHERE id =1";
line in place of the2
you have (or do it via a SQL parameter). -
In function
chequedState()
, do not callsaveAssistence()
as you iterate through each checkbox. Instead, increment afaults
variable, so that at the end of the loop it holds the total number of faults. Then callsaveAssistence(faults)
once at the end.
@JonB Thank you for the answer. Now, how can i do for do it with the parameter in sql. Instead this,
consult.append("UPDATE student SET faults = 2 WHERE id =1");
do it somehow like this:
consult.append("UPDATE student SET faults = +faults+");
i try it but i don't find the way to do it well. i want that when i uncheck a checkbox, the faults increments 1 when i save the state.
Thank you very much! -
-
@JonB Thank you for the answer. Now, how can i do for do it with the parameter in sql. Instead this,
consult.append("UPDATE student SET faults = 2 WHERE id =1");
do it somehow like this:
consult.append("UPDATE student SET faults = +faults+");
i try it but i don't find the way to do it well. i want that when i uncheck a checkbox, the faults increments 1 when i save the state.
Thank you very much!@Aioria
Hi
Please see documentation for using parameters
http://doc.qt.io/qt-5/qsqlquery.html
section Approaches to Binding Values -
Thank you! i can do the UPDATE, but i can't make it work yet.
void MainWindow::saveAssistence(int faults, int id) { QString consult; consult.append("UPDATE students SET faults = '"+QString::number(faults)+"' WHERE id = '"+QString::number(id)+"'"); // QSqlQuery update; update.prepare(consult); if(update.exec()){ qDebug()<<"the student is successful update."; }else{ qDebug()<<"the student is NOT successful update."; qDebug()<<"ERROR! " << update.lastError(); } }
void MainWindow::chequedState(){ int faults=0; int id = 1; int rowCount = ui->listWidget->count(); for (int i=0; i<rowCount; i++) { if(ui->listWidget->item(i)->checkState() == Qt::Unchecked){ faults++; saveAssistence(faults, id); } } }
The thing is that when i want to do the increment to the value, i cant do it. I have 4 students and 4 checkboxes, i want to add 1 value to faults when i uncheck the checkboxes and save the state, in the save button i call chequedState function. But i can't do this work. I tried putting the function out of the for but its the same result. Also i try to use just the parameter "Faults " but don't work.
-
Hi
Check the return value of both prepare and exec()
to see if it reports anything.
if exec() fails, you can obtain more information with
http://doc.qt.io/qt-5/qsqlquery.html#lastError -
Thank you! i can do the UPDATE, but i can't make it work yet.
void MainWindow::saveAssistence(int faults, int id) { QString consult; consult.append("UPDATE students SET faults = '"+QString::number(faults)+"' WHERE id = '"+QString::number(id)+"'"); // QSqlQuery update; update.prepare(consult); if(update.exec()){ qDebug()<<"the student is successful update."; }else{ qDebug()<<"the student is NOT successful update."; qDebug()<<"ERROR! " << update.lastError(); } }
void MainWindow::chequedState(){ int faults=0; int id = 1; int rowCount = ui->listWidget->count(); for (int i=0; i<rowCount; i++) { if(ui->listWidget->item(i)->checkState() == Qt::Unchecked){ faults++; saveAssistence(faults, id); } } }
The thing is that when i want to do the increment to the value, i cant do it. I have 4 students and 4 checkboxes, i want to add 1 value to faults when i uncheck the checkboxes and save the state, in the save button i call chequedState function. But i can't do this work. I tried putting the function out of the for but its the same result. Also i try to use just the parameter "Faults " but don't work.
-
You have not followed my/ @mrjj's suggestion of "binding values" to pass the two parameters to the SQL query. You should really read the link he gave you and do it the "proper" way described there.
-
In the way you do it: Originally you passed the two parameters as numbers
UPDATE student SET faults = 2 WHERE id =1
I note that that in the new code you will have'
(single-quote) characters around each number, so it will end up like
UPDATE student SET faults = '2' WHERE id ='1'
Depending on your SQL server, that may not work. -
You have left the
saveAssistence(faults, id);
statement inside thefor
loop. I said to do it once at the end, outside thefor
loop. Does it not make sense to you to do it like that?
-
-
Yes, i read it and i use the UPDATE because works to increment the values, if you read what i said, i tried to put the saveAsistance outside of the for but don't do nothing. I don't find anything about UPDATE in the Binding Values, just INSERT
@Aioria
First let's deal with the use of binding variables. The fact that the examples only tend to show it being used withINSERT
is not relevant. They can be used with any SQL statement, be thatSELECT
,INSERT
,UPDATE
,DELETE
or whatever, so long as you goprepare()
, thenbindValue()
, thenexec()
. It's just a neater way of getting a variable safely into the statement in the right format in the right place than putting it directly into the string via something likeQString::number()
.Now your issue. It's not your fault, but I/we find the description of what you are trying to achieve difficult to follow. From the way you want your
exec()
to be inside thefor
loop, I now have a feeling you want (potentially) multiple rows to each be updated separately. Does the following describe your situation:- You have some existing rows in the SQL table.
- Each row has an
id
, and afaults
counter of its own. - When you go through the checkboxes: if a given row's checkbox is unchecked, you want just that row's
faults
column to be incremented by 1.
For example:
- Row number
2
currently has the value5
stored in itsfaults
in the database. The teacher does not check its checkbox. So that means you want that row'sfaults
to be incremented to6
. - Meanwhile, another row with value
0
also has its checkbox unchecked, so that row wants it incremented to1
. - And a third row has its checkbox checked, so you do not want that row's
faults
incremented at all.
Is that it?
In this case, the secret is that you can write your SQL query so that the database side does the incrementing for you, using whatever
faults
value a given row (specified byid
) has currently to add to. Your Qt client does not need to know the number of faults already there. Something like: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() } }
-
@JonB
Thank you for the answer. I have another question. I tried that but not made changes. I have to reescribe the entire code about CREATE the table? I do that with QString. Maybe i need to do it with QSqlQuery. Here is the code:void MainWindow::CreateStudentTable() { QString consult; consult.append("CREATE TABLE IF NOT EXISTS students(" "id INTEGER PRIMARY KEY AUTOINCREMENT," "name VARCHAR(100)," "lastName VARCHAR(100), " "faults INTEGER NOT NULL" ");"); QSqlQuery create; create.prepare(consult); if(create.exec()){ qDebug()<<"The table STUDENT exists."; }else{ qDebug()<<"The table STUDENTS doesnt exists"; qDebug()<<"ERROR! " << create.lastError(); } }
-
@JonB
Thank you for the answer. I have another question. I tried that but not made changes. I have to reescribe the entire code about CREATE the table? I do that with QString. Maybe i need to do it with QSqlQuery. Here is the code:void MainWindow::CreateStudentTable() { QString consult; consult.append("CREATE TABLE IF NOT EXISTS students(" "id INTEGER PRIMARY KEY AUTOINCREMENT," "name VARCHAR(100)," "lastName VARCHAR(100), " "faults INTEGER NOT NULL" ");"); QSqlQuery create; create.prepare(consult); if(create.exec()){ qDebug()<<"The table STUDENT exists."; }else{ qDebug()<<"The table STUDENTS doesnt exists"; qDebug()<<"ERROR! " << create.lastError(); } }
@Aioria said in Sqlite change Row Value:
I tried that but not made changes
What do you mean? Was the table not created? What does create.lastError() return?
-
@Aioria said in Sqlite change Row Value:
I tried that but not made changes
What do you mean? Was the table not created? What does create.lastError() return?
I mean that i do this
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() } }
And when i want to save the state, don't increment the faults. I think that the problem is in ID and Faults but i can't make it work
-
I mean that i do this
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() } }
And when i want to save the state, don't increment the faults. I think that the problem is in ID and Faults but i can't make it work
@Aioria
In principle this code looks correct/reasonable. Don't understand what you say is the problem? Does the above not increment correctly. You should check the return result ofexec()
at least in case there is an error? Print out the value ofi
and make sure that is the desiredid
in the table. Otherwise do you have anything like SQL Workbench for SQLite where you can test these statements? -
@Aioria
In principle this code looks correct/reasonable. Don't understand what you say is the problem? Does the above not increment correctly. You should check the return result ofexec()
at least in case there is an error? Print out the value ofi
and make sure that is the desiredid
in the table. Otherwise do you have anything like SQL Workbench for SQLite where you can test these statements?@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.
-
@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.
@Aioria said in Sqlite change Row Value:
I don't have Workbench
You can simply use the command line tool delivered together with SQLite.
-
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