Hi,
For this you need a database connection. So you need:
Code in the header of the class (for example mainwindow.h)
#include <QtSql>
...
private:
...
#include <QtSql>
...
private:
QSqlDatabase db;
...
To copy to clipboard, switch view to plain text mode
Code in the implementation file (cpp)
db
= QSqlDatabase::addDatabase("QSQLITE",
"dblite");
//Add a database connector to SQLite. You just need to call this once. Put it in the constructor for example
//Process for validating the user and pass using a table in sqlite
db.close();
db.setDatabaseName("./myDatabase.sqlite");
if (!db.open()) //Try to opens the database
{
QMessageBox::critical(0, qApp
->tr
("Cannot open database"),
qApp->tr("Unable to establish a SQLite connection to the database\n\n"
}
else
{
QSqlQuery querylite
(db
);
//Create a query using db sql = "SELECT count(userName) from MyUsersTable WHERE userName = '";
sql = sql + ui->lineEdit->text() + "'";
sql = sql + " AND password = '";
sql = sql + ui->lineEdit2->text() + "'";
if (querylite.exec(sql)) //Executed the statement
{
if (querylite.value(0).toInt() > 0) //If the return is bigger that 0
{
//User and pass was found
}
else
{
//No access
}
}
else
{
//Error with the sql
}
}
db.close();
db = QSqlDatabase::addDatabase("QSQLITE","dblite"); //Add a database connector to SQLite. You just need to call this once. Put it in the constructor for example
//Process for validating the user and pass using a table in sqlite
db.close();
db.setDatabaseName("./myDatabase.sqlite");
if (!db.open()) //Try to opens the database
{
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a SQLite connection to the database\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
}
else
{
QSqlQuery querylite(db); //Create a query using db
QString sql; //SQL statement
sql = "SELECT count(userName) from MyUsersTable WHERE userName = '";
sql = sql + ui->lineEdit->text() + "'";
sql = sql + " AND password = '";
sql = sql + ui->lineEdit2->text() + "'";
if (querylite.exec(sql)) //Executed the statement
{
if (querylite.value(0).toInt() > 0) //If the return is bigger that 0
{
//User and pass was found
}
else
{
//No access
}
}
else
{
//Error with the sql
}
}
db.close();
To copy to clipboard, switch view to plain text mode
Carlos
Bookmarks