QT 使用sqlite的方法 實現使用者密碼登陸例項
阿新 • • 發佈:2019-01-06
#include "formlogin.h"
#include "ui_formlogin.h"
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlTableModel>
FormLogin::FormLogin(QWidget *parent) :
QDialog(parent),
ui(new Ui::FormLogin)
{
ui->setupUi(this);
this->setWindowTitle(QString::fromLocal8Bit("登入"));
if(!openDb())
{
createDb(); //如果不存在資料庫或無記錄,則建立
openDb();
}
initForm();
connect(ui->comboBoxUser,SIGNAL(currentIndexChanged(int)),SLOT(addPassword()));
connect(ui->buttonBox, SIGNAL(accepted()),SLOT(accept()));
connect(ui->buttonBox, SIGNAL(rejected()),SLOT(reject()));
}
FormLogin::~FormLogin()
{
delete ui;
}
void FormLogin::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
bool FormLogin::openDb()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("ioconf.db");
if(!db.open())
{
QMessageBox::warning(0, tr("Warning"), db.lastError().text());
return false;
}
QSqlQuery query(db);
if(!query.exec("select UserName,Password from users"))
{
db.close();
return false;
}
user_map.clear();
while(query.next())
{
QString UserName = query.value(0).toString();
QString Password = query.value(1).toString();
user_map.insert(UserName,Password);
}
db.close();
//判斷資料庫是否為空
if(user_map.isEmpty())
return false;
return true;
}
void FormLogin::createDb()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("ioconf.db");
if(db.open())
{
QString sqlstr="create table users(UserName text not null,Password text not null)";
QSqlQuery query(db);
query.exec(sqlstr);
query.prepare("insert into users (UserName, Password) values(:UserName, :Password)");
query.bindValue(":UserName", QString::fromLocal8Bit("系統管理員"));
query.bindValue(":Password", "system");
query.exec();
query.clear();
query.prepare("insert into users (UserName, Password) values(:UserName, :Password)");
query.bindValue(":UserName", QString::fromLocal8Bit("運維人員"));
query.bindValue(":Password", "admin");
query.exec();
query.clear();
}
}
void FormLogin::initForm()
{
QMap<QString,QString>::iterator it; //遍歷map
for ( it = user_map.begin(); it != user_map.end(); ++it )
{
ui->comboBoxUser->insertItem(0,it.key());
}
ui->lineEditPassword->setText(user_map[QString::fromLocal8Bit("系統管理員")]);
}
void FormLogin::addPassword()
{
int indexNo = 0;
indexNo = ui->comboBoxUser->currentIndex();
ui->lineEditPassword->setText(user_map[ui->comboBoxUser->currentText()]);
}
#include "ui_formlogin.h"
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlTableModel>
FormLogin::FormLogin(QWidget *parent) :
QDialog(parent),
ui(new Ui::FormLogin)
{
ui->setupUi(this);
this->setWindowTitle(QString::fromLocal8Bit("登入"));
if(!openDb())
{
createDb(); //如果不存在資料庫或無記錄,則建立
openDb();
}
initForm();
connect(ui->comboBoxUser,SIGNAL(currentIndexChanged(int)),SLOT(addPassword()));
connect(ui->buttonBox, SIGNAL(accepted()),SLOT(accept()));
connect(ui->buttonBox, SIGNAL(rejected()),SLOT(reject()));
}
FormLogin::~FormLogin()
{
delete ui;
}
void FormLogin::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
bool FormLogin::openDb()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("ioconf.db");
if(!db.open())
{
QMessageBox::warning(0, tr("Warning"), db.lastError().text());
return false;
}
QSqlQuery query(db);
if(!query.exec("select UserName,Password from users"))
{
db.close();
return false;
}
user_map.clear();
while(query.next())
{
QString UserName = query.value(0).toString();
QString Password = query.value(1).toString();
user_map.insert(UserName,Password);
}
db.close();
//判斷資料庫是否為空
if(user_map.isEmpty())
return false;
return true;
}
void FormLogin::createDb()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("ioconf.db");
if(db.open())
{
QString sqlstr="create table users(UserName text not null,Password text not null)";
QSqlQuery query(db);
query.exec(sqlstr);
query.prepare("insert into users (UserName, Password) values(:UserName, :Password)");
query.bindValue(":UserName", QString::fromLocal8Bit("系統管理員"));
query.bindValue(":Password", "system");
query.exec();
query.clear();
query.prepare("insert into users (UserName, Password) values(:UserName, :Password)");
query.bindValue(":UserName", QString::fromLocal8Bit("運維人員"));
query.bindValue(":Password", "admin");
query.exec();
query.clear();
}
}
void FormLogin::initForm()
{
QMap<QString,QString>::iterator it; //遍歷map
for ( it = user_map.begin(); it != user_map.end(); ++it )
{
ui->comboBoxUser->insertItem(0,it.key());
}
ui->lineEditPassword->setText(user_map[QString::fromLocal8Bit("系統管理員")]);
}
void FormLogin::addPassword()
{
int indexNo = 0;
indexNo = ui->comboBoxUser->currentIndex();
ui->lineEditPassword->setText(user_map[ui->comboBoxUser->currentText()]);
}