How to Validate username and password from postgreSQL database ?[SOLVED]
-
I have created database 'creatordb' and table name 'creator_tbl'
uresname | password
nandi | 1234
nandini | 123in VS2008
#include "create.h"
#include <QMessageBox>
#include <QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QtSql>
#include <string>Create::Create(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
}Create::~Create()
{}
void Create::on_pushButton_Clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");db.setHostName("localhost");
db.setDatabaseName("creatordb");
db.setUserName("postgres");
db.setPassword("pgadmin@123");
db.setPort(5432);
db.open();
bool ok=db.open();
if(ok!=true )
{
QSqlQueryModel model;
model.setQuery("select * from creator_tbl ");
QMessageBox Msgbox;
Msgbox.setText(" Failed ");
Msgbox.exec();
}
else{
QMessageBox Msgbox;
Msgbox.setText(" connected ");
Msgbox.exec();QSqlQueryModel model;
model.setQuery("select username from creator_tbl");
Msgbox.setText("Hi...!!! Welcome you had successfully logged in to QuickWorker Task Management");
Msgbox.exec();
newCreate=new NewCreate();
this->hide();newCreate->show();
}
}If i change the database name ,something like 'creatordb' to 'creatordb1' it gives a messagebox of FAILED.This s how i come to kn it is connected to database.
If i give the wrong username and password also "Hi...!!! Welcome you had successfully logged in to QuickWorker Task Management" messagebox is displayed.
Please help me with the code to validate ursname and psswd. I mean if i enter anything other than what db contains it should throw me ERROR saying 'data not matched'!
-
Hi,
Please enclose your code with coding tags, it will make it readable. You seem to call open on db twice and another strange thing, if open fails you create a model ?
-
@#include “create.h”
#include <QMessageBox>
#include <QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QtSql>
#include <string>Create::Create(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
{
ui.setupUi(this);
}Create::~Create()
{}
void Create::on_pushButton_Clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase(“QPSQL”);db.setHostName(“localhost”);
db.setDatabaseName(“creatordb”);
db.setUserName(“postgres”);
db.setPassword(“pgadmin@123”);
db.setPort(5432);
db.open();
bool ok=db.open();
if(ok!=true )
{
QSqlQueryModel model;
model.setQuery(“select * from creator_tbl “);
QMessageBox Msgbox;
Msgbox.setText(” Failed “);
Msgbox.exec();
}
else
{
QMessageBox Msgbox;
Msgbox.setText(” connected “);
Msgbox.exec();
QSqlQueryModel model;
model.setQuery(“select username from creator_tbl”); Msgbox.setText(“Hi…!!! Welcome you had successfully logged in to QuickWorker Task Management”);Msgbox.exec();
newCreate=new NewCreate();
this->hide();
newCreate->show();
}
}
@On giving incorrect database name it executes
@Msgbox.setText(” Failed “);@and on giving correct db name i.e 'creatordb' it executes
@Msgbox.setText(“Hi…!!! Welcome you had successfully logged in to QuickWorker Task Management”);@
so my connection to db is correct isn't it?
Please help me with this- how to validate username and password by fetching the data from the table?
-
Hi,
First as SGaist said you are opening the database twice here. Its useless.
@
db.open();
bool ok=db.open();
@Also you can use QSqlQuery directly instead to execute your query where you will fetch the required data from db.
So for eg. thequery may go like this,
@
QSqlQuery query(QSqlDatabase::database());
QString q = "select id from creator_tbl where username='testuser'";
bool success = query.exec(q); //if there is no match success == falsequery.next(); //if required
QString id = query.value(0).toString(); // if requiredif(success)
qDebug() << "User validated" << id;
else
qDebug() << "User not validated";
@ -
Now i have this-
@else{
QString username=ui.lineEdit_4->text();
QString password=ui.lineEdit_3->text();
QMessageBox Msgbox;
Msgbox.setText(" connected ");
Msgbox.exec();QSqlQuery query(QSqlDatabase::database());
QString q = "select username,password from creator_tbl where username='"+username+"' and password='"+password+"'";
bool success = query.exec(q);if(success)
{
Msgbox.setText(" data in the table is matched");
Msgbox.exec();
newCreate=new NewCreate();
this->hide();
newCreate->show();//this is my 2nd frame
}
else
{
Msgbox.setText("data NOT matched");
Msgbox.exec();
}
}@I want data to be matched implicitly, instead of this-
@QString q = "select id from creator_tbl where username='testuser'";@Following my code ,in the "login" page on entering usrname and password(randomly)
- Tells me db is connected
@Msgbox.setText(" connected ");@
2.Data in table is matched
@Msgbox.setText(" data in the table is matched");@
Even though the data what i have entered in lineEdit_4->text()[usrname] and lineEdit_3[passwd]->text() are incorrect @if(success) @ is returning TRUE.
ALL i want is to COMPARE with database table values !! wats wrong or wat am i missing here?
//qDebug()<<" ----"; is not all working so i am using .setText("------");
- Tells me db is connected
-
Oops sorry.
Try
@
query.exec(q);
int s = query.size();
bool success = (s<=0) ? false:true;
@So if any results are returned "success" becomes true as size will return number of rows affected or -1 any problem.
-
You're Welcome :) You can mark it as solved by editing and prepending [solved] to the question.