QT使用SQLite數(shù)據(jù)庫超詳細教程(增刪改查、對大量數(shù)據(jù)快速存儲和更新)
QT+SQLite
在QT中使用sqlite數(shù)據(jù)庫,有多種使用方法,在這里我只提供幾種簡單,代碼簡短的方法,包括一些特殊字符處理。在這里也給大家說明一下,如果你每次要存儲的數(shù)據(jù)量很大,建議使用事務(wù)(代碼中有體現(xiàn)),萬條數(shù)據(jù)不到一秒吧。
用SQlite建立一個簡單學(xué)生管理數(shù)據(jù)庫
數(shù)據(jù)庫中有兩個表一個是class和student。

class表結(jié)構(gòu)

student表結(jié)果

創(chuàng)建工程
我的工程如下:

直接上代碼(看注釋更通透)
student.pro文件添加sql模塊。
QT += core gui
QT += sql #添加數(shù)據(jù)庫模塊
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
Student.cpp
HEADERS += \
Student.h
FORMS += \
Student.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
student.h文件添加相關(guān)定義。
#ifndef STUDENT_H
#define STUDENT_H
#include <QWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlRecord>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Student; }
QT_END_NAMESPACE
class Student : public QWidget
{
Q_OBJECT
public:
Student(QWidget *parent = nullptr);
~Student();
//定義一個變量,用于增刪改查
QString queryString;
void Add();//添加數(shù)據(jù),不支持大量數(shù)據(jù)快速添加
void Delete();//刪除數(shù)據(jù),支持大量數(shù)據(jù)快速刪除
void Update();//更新數(shù)據(jù),若更新大量數(shù)據(jù),可以先快速刪除后在快速添加
void Select();//查詢數(shù)據(jù),支持大量數(shù)據(jù)快速查詢
void FastAdd();//在sqlite中快速添加大量數(shù)據(jù)
private:
Ui::Student *ui;
QSqlDatabase DB;//定義數(shù)據(jù)庫名稱
};
#endif // STUDENT_Hstudent.cpp文件編輯。(值得一提的是,在數(shù)據(jù)庫刪除操作中,有時候要判斷限制條件是否為空,在代碼中已體現(xiàn))
#include "Student.h"
#include "ui_Student.h"
Student::Student(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Student)
{
ui->setupUi(this);
//打開數(shù)據(jù)庫
DB = QSqlDatabase::addDatabase("QSQLITE");
DB.setDatabaseName("./StudentDB.db");//打開數(shù)據(jù)庫
if (DB.open())
{
qDebug() << "Database opened successfully!";
}
else
{
qDebug() << "無法打開數(shù)據(jù)庫:" << DB.lastError().text();
}
Add();//添加數(shù)據(jù)
Select();//查詢數(shù)據(jù)
Update();//更新數(shù)據(jù)
Delete();//刪除數(shù)據(jù)
FastAdd();//對大量數(shù)據(jù)進行快速添加,嘗試過添加幾千條數(shù)據(jù),不到一秒就添加完成
}
Student::~Student()
{
delete ui;
}
void Student::Add()//增
{
/*
* 在班級表中添加數(shù)據(jù),
添加的數(shù)據(jù)為
班級名稱:一班
班主任:李主任
班級人數(shù):25
*/
queryString = QString("insert into class(class_name, class_teacher ,student_number) values('%1','%2',%3) ")
.arg("一班").arg("李主任").arg(25);
QSqlQuery query;//執(zhí)行sql語句
if(query.exec(queryString)){
qDebug()<<"insert data Successful!";
}
else {
qDebug()<<"insert data Failed!";
}
}
void Student::Delete()//刪
{
//在這里有時候要判斷限制條件是否為空,這時候可以用這個sql語句,當班級名稱不為空時刪除
//queryString = QString("delete from class where class_name is not null");
queryString = QString("delete from class where class_name = '%1'").arg("一班");
QSqlQuery query(queryString);
if(query.exec()){
qDebug()<<"Delete data Successful!";
}
else {
qDebug()<<"Delete data Failed!";
}
}
void Student::Update()//改
{
//更新數(shù)據(jù),將班級名稱作為限制條件進行數(shù)據(jù)更新
queryString = QString("update class set class_teacher='%1' ,student_number=%2 where class_name='%3' ")
.arg("張主任").arg(30).arg("一班");
QSqlQuery query;
if (query.exec(queryString))
{
qDebug()<<"updata data Successful!";
}
else
{
qDebug()<<"updata data Failed!";
}
}
void Student::Select()//查
{
queryString = QString("select * from class where class_name = '%1'").arg("一班");
QSqlQuery query(queryString);
while(query.next()){
QString class_teacher = query.value("class_teacher").toString();
int student_number = query.value("student_number").toInt();
qDebug()<<"班主任:"<<class_teacher;
qDebug()<<"班級人數(shù):"<<student_number;
}
}
void Student::FastAdd()//對大量數(shù)據(jù)進行快速添加,嘗試過添加幾千條數(shù)據(jù),不到一秒就添加完成
{
//使用事務(wù)
DB.transaction();//開啟事務(wù)
QSqlQuery query;
query.prepare("INSERT INTO class (class_name, class_teacher, student_number) VALUES (?, ?, ?)");
for (int i = 0; i < 100; ++i) {
query.bindValue(0, "二班");
query.bindValue(1, "張三");
query.bindValue(2, i);
query.exec();
}
DB.commit();//一次性提交,省去大量時間
}運行完之后都出現(xiàn)successful,就說明,你已經(jīng)掌握了qt+sqlite的增刪改查。

總結(jié)
到此這篇關(guān)于QT使用SQLite數(shù)據(jù)庫(增刪改查、對大量數(shù)據(jù)快速存儲和更新)的文章就介紹到這了,更多相關(guān)QT使用SQLite增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++ Windows SAPI實現(xiàn)文字轉(zhuǎn)語音功能
本文通過封裝Windows SAPI(Speech Application Programming Interface),提供了一個現(xiàn)代化的C++接口實現(xiàn)文字轉(zhuǎn)語音功能,這篇文章重點給大家介紹C/C++ Windows SAPI自實現(xiàn)文字轉(zhuǎn)語音功能,感興趣的朋友一起看看吧2025-02-02
C++數(shù)據(jù)結(jié)構(gòu)之哈希算法詳解
這篇文章主要為大家詳細介紹了C++數(shù)據(jù)結(jié)構(gòu)中哈希算法的相關(guān)資料,文中的示例代碼講解詳細,具有一定的借鑒價值,希望對大家有所幫助2022-12-12
C++實現(xiàn)LeetCode(17.電話號碼的字母組合)
這篇文章主要介紹了C++實現(xiàn)LeetCode(17.電話號碼的字母組合),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07

