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

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

student表結(jié)果

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

直接上代碼(看注釋更通透)
student.pro文件添加sql模塊。
QT += core gui
QT += sql #添加數(shù)據(jù)庫(kù)模塊
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();
//定義一個(gè)變量,用于增刪改查
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ù)庫(kù)名稱
};
#endif // STUDENT_Hstudent.cpp文件編輯。(值得一提的是,在數(shù)據(jù)庫(kù)刪除操作中,有時(shí)候要判斷限制條件是否為空,在代碼中已體現(xiàn))
#include "Student.h"
#include "ui_Student.h"
Student::Student(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Student)
{
ui->setupUi(this);
//打開數(shù)據(jù)庫(kù)
DB = QSqlDatabase::addDatabase("QSQLITE");
DB.setDatabaseName("./StudentDB.db");//打開數(shù)據(jù)庫(kù)
if (DB.open())
{
qDebug() << "Database opened successfully!";
}
else
{
qDebug() << "無法打開數(shù)據(jù)庫(kù):" << DB.lastError().text();
}
Add();//添加數(shù)據(jù)
Select();//查詢數(shù)據(jù)
Update();//更新數(shù)據(jù)
Delete();//刪除數(shù)據(jù)
FastAdd();//對(duì)大量數(shù)據(jù)進(jìn)行快速添加,嘗試過添加幾千條數(shù)據(jù),不到一秒就添加完成
}
Student::~Student()
{
delete ui;
}
void Student::Add()//增
{
/*
* 在班級(jí)表中添加數(shù)據(jù),
添加的數(shù)據(jù)為
班級(jí)名稱:一班
班主任:李主任
班級(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語(yǔ)句
if(query.exec(queryString)){
qDebug()<<"insert data Successful!";
}
else {
qDebug()<<"insert data Failed!";
}
}
void Student::Delete()//刪
{
//在這里有時(shí)候要判斷限制條件是否為空,這時(shí)候可以用這個(gè)sql語(yǔ)句,當(dāng)班級(jí)名稱不為空時(shí)刪除
//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ù),將班級(jí)名稱作為限制條件進(jìn)行數(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()<<"班級(jí)人數(shù):"<<student_number;
}
}
void Student::FastAdd()//對(duì)大量數(shù)據(jù)進(jìn)行快速添加,嘗試過添加幾千條數(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();//一次性提交,省去大量時(shí)間
}運(yùn)行完之后都出現(xiàn)successful,就說明,你已經(jīng)掌握了qt+sqlite的增刪改查。

總結(jié)
到此這篇關(guān)于QT使用SQLite數(shù)據(jù)庫(kù)(增刪改查、對(duì)大量數(shù)據(jù)快速存儲(chǔ)和更新)的文章就介紹到這了,更多相關(guān)QT使用SQLite增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解VisualS tudio Code開發(fā)Arm嵌入式Linux應(yīng)用
本文介紹如何在 Visual Studio Code 中使用 Yocto Project 生成的 Linux SDK,并針對(duì) Arm 處理器進(jìn)行 C/C++ 應(yīng)用交叉編譯和調(diào)試,感興趣的朋友跟隨小編一起看看吧2021-04-04
C語(yǔ)言實(shí)現(xiàn)通訊錄系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)通訊錄系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
用C實(shí)現(xiàn)PHP擴(kuò)展 Image_Tool 圖片常用處理工具類的使用
該擴(kuò)展是基于ImageMagick基礎(chǔ)實(shí)現(xiàn)的,圖片操作調(diào)用的是ImageMagick API2013-04-04
c++實(shí)現(xiàn)一個(gè)簡(jiǎn)易的網(wǎng)絡(luò)緩沖區(qū)的實(shí)踐
這篇文章主要介紹了c++實(shí)現(xiàn)一個(gè)簡(jiǎn)易的網(wǎng)絡(luò)緩沖區(qū)的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
C++工廠方法之對(duì)象創(chuàng)建型模式詳解
這篇文章主要為大家詳細(xì)介紹了C++對(duì)象創(chuàng)建型模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03

