QT连接SQLite数据库(实现登陆注册)

大致简介

1、用SQLiteStudio构建 一个.db的数据库

2、前期准备:常用头文件的引入

#include  //用于创建连接并打开数据库
#include 
#include 
#include  //执行查询操作

3、将构造函数私有化

private: //构造函数私有化
    explicit database(QObject *parent = nullptr);
    static database *userDb;

4、调用QSqlDatabase的静态函数database创建一个连接

QSqlDatabase db = QSqlDatabase::database("smart-home-db");

5、创建一个关联的QSqlQuery对象,由QSqlQuery来实现数据库的增删查改工作

QSqlQuery  query(db);
query.addBindValue(username);
query.addBindValue(password);
query.addBindValue(phone);

6、获取登陆界面中输入栏的数据,与数据库中的数据进行比对,正确则跳转到主界面

7、获取注册界面中的输入栏数据,检测数据库中是否有相同数据,若没有则存入


例子:用户的登陆注册 

数据库连接部分

----------
database.h
----------
#ifndef DATABASE_H
#define DATABASE_H

#include 

class database : public QObject
{
    Q_OBJECT
public:
    static database *getdatabase();
//函数声明
    bool addRegiserUser(const QString &username,const QString &password,const  QString &phone);
    bool queryphone(const QString &phone);
    bool queryusername(const QString &username);
    bool querypassword(const QString &username,const QString &password);

signals:

public slots:

private: //构造函数私有化
    explicit database(QObject *parent = nullptr);
    static database *userDb;
};

#endif // DATABASE_H

-------------
database.cpp
-------------
#include "database.h"
#include 
#include 
#include 
#include 

database* database::userDb = NULL;

database *database::getdatabase()
{
    if(!userDb){
        userDb = new database;
    } //new一个构造函数

    return userDb;
}

//注册功能
bool database::addRegiserUser(const QString &username, const QString &password, const QString &phone)
{
    //调用QSqlDatabase的静态函数databas创建了一个名为smart-home-db的连接
    QSqlDatabase db = QSqlDatabase::database("smart-home-db");
    
    //创建一个关联的QSqlQuery对象,由QSqlQuery来实现以下工作
    QSqlQuery  query(db);
    
    //将输入username、password、phone 的数据插入表中;
    query.prepare("INSERT INTO User (username,password,phone) "
                  "VALUES (?, ?, ?)");

    query.addBindValue(username);
    query.addBindValue(password);
    query.addBindValue(phone);
    bool ok = query.exec(); //判断打开数据库是否成功;
    if(!ok){
        qDebug() << "Fail add regiser user : " << query.lastError().text();
    }

    return ok;
}

//登陆的电话检测
bool database::database::queryphone(const QString &phone)
{
    QSqlDatabase db = QSqlDatabase::database("smart-home-db");
    QSqlQuery query(db);
    
    //判断数据库中是否存在所输入号码
    query.prepare("select *from User where phone=?");
    query.addBindValue(phone);
    bool ok = query.exec();
    if(!ok){
        qDebug() << "Fail query register phone" << db.lastError().text();
        return false;
    }
    if(query.next()){
        return true;
    }
    else{
        return false;
    }
}

//登陆的用户名检测
bool database::database::queryusername(const QString &username)
{
    QSqlDatabase db = QSqlDatabase::database("smart-home-db");
    QSqlQuery query(db);
    query.prepare("select *from User where username=?");
    query.addBindValue(username);
    bool ok = query.exec();
    if(!ok){
        qDebug() << "Fail query register username" << db.lastError().text();
        return false;
    }
    if(query.next()){
        return true;
    }
    else{
        return false;
    }
}


//登陆的密码检测
bool database::database::querypassword(const QString &username,const QString &password)
{
    QSqlDatabase db = QSqlDatabase::database("smart-home-db");
    QSqlQuery query(db);
    query.prepare("select *from User where username=? and password=?");
    query.addBindValue(username);
    query.addBindValue(password);
    bool ok = query.exec();
    if(!ok){
        qDebug() << "Fail query register password" << db.lastError().text();
        return false;
    }
    if(query.next()){
        return true;
    }
    else{
        return false;
    }
}

database::database(QObject *parent) : QObject(parent)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","smart-home-db");
    db.setDatabaseName("/Users/dongjiahuizi/Desktop/dong.db"); //数据库文件路径
    bool ok = db.open(); //打开数据库
    if(!ok){
       qDebug() << "Fail to open database,error:" ;/*<< db.lastError.text();*/
    } //检测输出
}

登陆部分

----------
login.cpp
----------
#include "login.h"
#include "ui_login.h"
#include "majorwindow.h"
#include "registerwindow.h"
#include "database.h"
#include 
#include 
#include 

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    re = new registerwindow;
}

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

void Login::on_pushButton_3_clicked()
{
    MajorWindow *major = new MajorWindow;
    //获取用户名输入框的数据
    QString username = ui->lineEdit->text();
    //获取密码输入框的数据
    QString password = ui->lineEdit_2->text();

    database *userDb = database::getdatabase();
    if(userDb->queryusername(username)){
        //在数据库中查询到了就跳转到主页
        if(userDb->querypassword(username,password)){
            major->show();//跳转到major页面
            this->close(); //关闭登陆页面
        }
        //在数据库中查询不到即弹出信息错误窗口
        else{
            QMessageBox::information(NULL,"登录","密码错误",QMessageBox::Ok);
            return;
        }
    }
    else{
        QMessageBox::information(NULL,"登录","用户名不存在",QMessageBox::Ok);
        return;
    }
}

//记住密码框,点击显示小勾
void Login::on_pushButton_2_clicked()
{
    if(flag == true){
        ui->pushButton->setText(" ");
        flag = false;
    }
    else{
        ui->pushButton->setText("√");
        flag = true;
    }
}

//用户注册页面跳转
void Login::on_pushButton_5_clicked()
{
    re->show();
}

----------
login.h
----------
#ifndef LOGIN_H
#define LOGIN_H

#include 
#include "registerwindow.h"

namespace Ui {
class Login;
}

class Login : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_pushButton_3_clicked();
    void on_pushButton_2_clicked();
    void on_pushButton_5_clicked();

private:
    Ui::Login *ui;
    bool flag = false;
    registerwindow *re; //注册页面
};

#endif // LOGIN_H

注册部分

----------
register.h
----------
#ifndef REGISTERWINDOW_H
#define REGISTERWINDOW_H
#include 

namespace Ui {
class registerwindow;
}

class registerwindow : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();

private:
    Ui::registerwindow *ui;
};

#endif // REGISTERWINDOW_H

-------------
register.cpp
-------------
#include "registerwindow.h"
#include "ui_registerwindow.h"
#include "database.h"
#include 
#include 
#include 

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

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

void registerwindow::on_pushButton_clicked()
{
    //获取输入的用户名、密码、电话分别存入username、password、phone对象中;
    QString username = ui->lineEdit->text();
    QString password = ui->lineEdit_2->text();
    QString phone = ui->lineEdit_3->text();

//控制台输出测试部分
    qDebug() << "username : " << username;
    qDebug() << "password : " << password;
    qDebug() << "phone    : " << phone;

//
    database *userDb = database::getdatabase();

//判断用户名在数据库中是否存在,违规弹出注册失败弹窗
    if(userDb->queryusername(username)){
        QMessageBox::information(NULL,"注册","用户名已被注册",QMessageBox::Ok);
        return;
    }
//判断手机号在数据库中是否存在,违规弹出注册失败弹窗
    if(userDb->queryphone(phone)){
        QMessageBox::information(NULL,"注册","手机号已被注册",QMessageBox::Ok);
        return;
    }
//打开数据库
    bool ok = userDb->addRegiserUser(username,password,phone);
    if(!ok){
        QMessageBox::information(NULL,"注册","注册失败",QMessageBox::Ok);
    }else{
        QMessageBox::information(NULL,"注册","注册成功",QMessageBox::Ok);
    }
}

 

你可能感兴趣的:(QT)