一、简介
1.1 SQLite(sql)是一款开源轻量级的数据库软件,不需要server,可以集成在其他软件中,非常适合嵌入式系统。Qt5以上版本可以直接使用SQLite(Qt自带驱动)。
二、下载和配置
2.1 SQLite下载官网下载链接
2.2 根据计算机的配置,选择所需项目是64位还是32位下载对应的压缩包
2.3 安装
在系统盘(C盘)建一个名称为“sqlite”的文件夹,把上面下载的两个压缩包解压到这个sqlite文件夹中
2.4 配置计算机环境变量
右键此电脑,点击“属性”,打开“高级系统设置。
2.5.验证安装是否成功
三、应用数据库主要是增删查改,下面是相关函数代码
3.1 首先包含相关库文件
#include <QtWidgets/QWidget>
#include <sstream>
#include <QString>
#include <QDomDocument>
#include <QTextStream>
#include <QFile>
#include <QDir>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QMessageBox>
#include <vector>
3.2 函数
#pragma region 创建并连接SQLite数据库
//创建并连接SQLite数据库
bool SQLite::CreateConnectDB(QString sql_name, QSqlDatabase &database)
{try{//建立并打开数据库database = QSqlDatabase::addDatabase("QSQLITE");database.setDatabaseName(sql_name);bool bol_sql_open = database.open();if (bol_sql_open == false){QMessageBox::critical(NULL, "Prompt", database.lastError().text());return false;}else{return true;}}catch (...){return false;}
}
#pragma endregion#pragma region 创建表格
//创建表格
bool SQLite::CreateTable(QString table_name)
{try{QSqlQuery sql_query;QString sql = "create table " + table_name + "(USERNAME text, PASSWORD text)";if (!sql_query.exec(sql)){QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());return false;}else{return true;}}catch (...){return false;}
}
#pragma endregion#pragma region 插入数据
//插入数据
bool SQLite::InsertData(QString table_name, QString user_name, QString password)
{try{QSqlQuery sql_query;QString sql = "insert into " + table_name + " values (?, ?)";sql_query.prepare(sql);sql_query.addBindValue(user_name);sql_query.addBindValue(password);if (!sql_query.exec()){QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());return false;}else{return true;}}catch (...){return false;}
}
#pragma endregion#pragma region 修改数据
//修改数据
bool SQLite::ModifyData(QString user_name, QString password)
{try{QSqlQuery sql_query;QString sql = "update LoginTable set PASSWORD = :PASSWORD where USERNAME = :USERNAME";sql_query.prepare(sql);sql_query.bindValue(":USERNAME", user_name);sql_query.bindValue(":PASSWORD", password);if (!sql_query.exec()){QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());return false;}else{return true;}}catch (...){return false;}
}
#pragma endregion#pragma region 查询数据
//查询数据
bool SQLite::QueryData(QString user_name, QString &password)
{try{//eg: "select * from LoginTable where USERNAME == 'user0';"QSqlQuery sql_query("select * from LoginTable where USERNAME == '" + user_name + "';");if (!sql_query.exec()){QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());return false;}else{while (sql_query.next()){password = sql_query.value(1).toString();}return true;}}catch (...){return false;}
}
#pragma endregion#pragma region 删除数据
//删除数据
bool SQLite::DeleteData(QString user_name)
{try{QSqlQuery sql_query("delete from LoginTable where USERNAME == '" + user_name + "';");if (!sql_query.exec()){QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());return false;}else{return true;}}catch (...){return false;}
}
#pragma endregion#pragma region 删除表格
//删除表格
bool SQLite::DeleteTable(QString table_name)
{try{QSqlQuery sql_query;sql_query.exec("drop table " + table_name);if (sql_query.exec()){QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());return false;}else{return true;}}catch (...){return false;}
}
#pragma endregion#pragma region 关闭数据库
//关闭数据库
bool SQLite::CloseDB( QSqlDatabase database)
{try{database.close();return true;}catch (...){return false;}
}
#pragma endregion