QT SQL DATABASE LOGIN
-
hello, am new in application development.
I have connected my application to sql database and am able to enter data into the database through the user interface. But I want to make sure that a user does not enter same type of data twice.I have tried to use the if and the else statement,but the problem is that the else is not working, what could be the problem? I will appreciate your help. below is the code:```
#include "schoolfee.h"
#include "ui_schoolfee.h"schoolFee::schoolFee(QWidget *parent) :
QDialog(parent),
ui(new Ui::schoolFee)
{
ui->setupUi(this);
ui->SURNAME->setPlaceholderText("ENTER SURNAME");
ui->GRADE->setPlaceholderText("INTER GRADE, e.g 9");
ui->CLASS->setPlaceholderText("ENTER CLASS e.g 9A");
ui->AMOUNT->setPlaceholderText("ENTER AMOUNT e.g 100");
ui->OTHERNAME->setPlaceholderText("ENTER OTHER NAME(S)");
//ui->DATE->setPlaceholderText("e.g 01/01/2000");}
schoolFee::~schoolFee()
{
delete ui;}
void schoolFee::on_ENTER_FEE_clicked()
{QString name =ui->SURNAME->text(); QString grade =ui->GRADE->text(); QString clas= ui->CLASS->text(); QString amount=ui->AMOUNT->text(); QString receiver=ui->OTHERNAME->text(); QString date=ui->DATE->text(); if(name=="" || grade==""|| clas==""||amount==""||receiver==""||date=="") { QMessageBox::warning(this,"warning","PLEASE ENTER ALL THE REQUIRED DETAILS"); } else {
QSqlDatabase::database();
QSqlQuery query;
query.prepare(QString("SELECT SURNAME,LASTNAME FROM payments WHERE SURNAME=:SURNAME AND LASTNAME=:LASTNAME"));query.bindValue(":SURNAME",name);
query.bindValue(":LASTNAME",receiver);if(!query.exec()){
QMessageBox::warning(this,"warninng","PLEASE TURN ON THE SYSTEM!");
}
else
{while(query.next())
{
QString nameFromDB=query.value(0).toString();
QString receiverFromDB=query.value(1).toString();if(nameFromDB==name && receiverFromDB==receiver){ QMessageBox::warning(this,"WARNING","THE NAME HAS BEEN ENTERED ALREADY!!!\n" " USE INITIAL IF ITS A DIFFERENT NAME"); break; } else { QSqlQuery qry; qry.prepare("INSERT INTO payments (SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE)" "VALUES(:SURNAME, :LASTNAME, :GRADE, :CLASS, :AMOUNT, :DATE)"); qry.bindValue(":SURNAME",name); qry.bindValue(":GRADE",grade); qry.bindValue(":CLASS",clas); qry.bindValue(":AMOUNT",amount); qry.bindValue(":LASTNAME",receiver); qry.bindValue(":DATE",date); if(qry.exec()) { QMessageBox::information(this,"inserted","PAYMENTS RECORDED SUCCESSFULLY!"); } else
{
QMessageBox::warning(this,"warning","PAYMENT NOT RECORDED!");
}
}
}
}
}}
-
@Christian-Ehrlicher
Okay , thanks for the information,but am new here,how can it mark as solved please? -
This part:```
else
{QSqlQuery qry; qry.prepare("INSERT INTO payments (SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE)" "VALUES(:SURNAME, :LASTNAME, :GRADE, :CLASS, :AMOUNT, :DATE)"); qry.bindValue(":SURNAME",name); qry.bindValue(":GRADE",grade); qry.bindValue(":CLASS",clas); qry.bindValue(":AMOUNT",amount); qry.bindValue(":LASTNAME",receiver); qry.bindValue(":DATE",date); if(qry.exec()) { QMessageBox::information(this,"inserted","PAYMENTS RECORDED SUCCESSFULLY!"); } else
{
QMessageBox::warning(this,"warning","PAYMENT NOT RECORDED!");
}
}
}
}
} -
Hi,
That's the kind of constraints that should be done at the database level.
Your database structure might also be too simple in the sense that if can be pretty easy to get duplicated data with small typos. -
@GREYON
You should say what goes wrong? Do you get a compilation error? A runtime error? A message? If so, tell us what the message is. Something doesn't do what you expect? If so what, and how do you know? That's what's required for people to look at your issue!qry.prepare("INSERT INTO payments (SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE)" "VALUES(:SURNAME, :LASTNAME, :GRADE, :CLASS, :AMOUNT, :DATE)");
Those two strings get joined. Look at them, and you will realize that will generate in the middle:
... CLASS,AMOUNT,DATE)VALUES(:SURNAME,
The
VALUES
touches the parenthesis to its left, which is different from the sample code at https://doc.qt.io/qt-5/qsqlquery.html#approaches-to-binding-values. I don't know if that will be enough to error.In any case, when a query goes wrong (if that's what's happening), use https://doc.qt.io/qt-5/qsqlquery.html#lastError to print out the text of the error.
-
@GREYON said in QT SQL DATABASE LOGIN:
@SGaist
Now what are you suggesting items of help?What experience do you have with relational database design ?
-
Well, in that case you should start by learning a bit more about them before going further. At least take the time to go through Qt's SQL module documentation and examples.
-
@GREYON Then please mark this topic as solved, thx.
-
@Christian-Ehrlicher
Okay , thanks for the information,but am new here,how can it mark as solved please?