Qt13 Creating connection open and close function with sqlite database

1.  drag a label widget on employeeinfo window, name as label_sec_status

2. modify login.h

#ifndef LOGIN_H
#define LOGIN_H




#include <QMainWindow>
#include <QtSql>
#include <QDebug>
#include <QFileInfo>

#include "employeeinfo.h"

namespace Ui {
class login;
}

class login : public QMainWindow
{
    Q_OBJECT

public:
    static void connClose()
    {
        connected = false;
        mydb.close();
        mydb = QSqlDatabase();
        mydb.removeDatabase(QSqlDatabase::defaultConnection);
    }
    static bool connOpen()
    {
        if( !connected )
        {
            mydb = QSqlDatabase::addDatabase("QSQLITE");
            mydb.setDatabaseName("D:/work_files/sqlite-tools-win32-x86-3120000/company.db");

            if(!mydb.open())
            {
                qDebug() << "Failed to open the database";
                connected = false;
            }
            else
            {
                qDebug() << "Connected...";
                connected = true;
            }
        }
        return connected;
    }

public:
    explicit login(QWidget *parent = 0);
    ~login();

private slots:
    void on_pushButton_clicked();

private:
    Ui::login *ui;
    static QSqlDatabase mydb;
    static bool connected;
};

#endif // LOGIN_H


3.modify login.cpp

#include "login.h"
#include "ui_login.h"

QSqlDatabase login::mydb;
bool login::connected = false;

login::login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);

    QPixmap pix("G:/TestQT/Sqlite_DB/icon/panda.png");
    ui->label_pic->setPixmap(pix);

    if(!connOpen())
    {
        ui->label->setText("Failed to open the database") ;

    }
    else
    {
        ui->label->setText("Connected...") ;

    }

}

login::~login()
{
    delete ui;
}

void login::on_pushButton_clicked()
{
    QString username, password;
    username = ui->lineEdit_Username->text();
    password = ui->lineEdit_Password->text();

    if(!connOpen())
    {
        ui->label->setText("Failed to open the database");
        return ;
    }

    QSqlQuery qry;

    QString stmt = "select * from employeeinfo where username='"+ username +"' and password='"+ password +"'";
    qry.prepare(stmt);
    if( qry.exec())
    {
        int count = 0;
        while(qry.next())
            count++;
        if( count == 1 )
        {
            ui->label->setText("username and password is correct");
            connClose();
            this->hide();
            EmployeeInfo employeeinfo;
            employeeinfo.setModal(true);
            employeeinfo.exec();

        }
        else if( count > 1 )
            ui->label->setText("Duplicate username and password");
        if( count < 1 )
            ui->label->setText("username and password is not correct");
    }
    else
    {
        qDebug() << qry.lastError();
    }
}

4. modify employeeinfo.cpp

#include "employeeinfo.h"
#include "ui_employeeinfo.h"
#include "login.h"
EmployeeInfo::EmployeeInfo(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EmployeeInfo)
{
    ui->setupUi(this);
    if(!login::connOpen())
    {
        ui->label_sec_status->setText("Failed to open the database") ;

    }
    else
    {
        ui->label_sec_status->setText("Connected...") ;

    }
}

EmployeeInfo::~EmployeeInfo()
{
    delete ui;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值