最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

QT使用SQLite數(shù)據(jù)庫超詳細教程(增刪改查、對大量數(shù)據(jù)快速存儲和更新)

 更新時間:2024年01月12日 08:54:20   作者:椿融雪  
這篇文章主要給大家介紹了關(guān)于QT使用SQLite數(shù)據(jù)庫的相關(guān)資料,其中包括增刪改查以及對大量數(shù)據(jù)快速存儲和更新,SQLite是一種嵌入式關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它是一個軟件庫,提供了一個自包含、無服務(wù)器、零配置的、事務(wù)性的SQL數(shù)據(jù)庫引擎,需要的朋友可以參考下

QT+SQLite

在QT中使用sqlite數(shù)據(jù)庫,有多種使用方法,在這里我只提供幾種簡單,代碼簡短的方法,包括一些特殊字符處理。在這里也給大家說明一下,如果你每次要存儲的數(shù)據(jù)量很大,建議使用事務(wù)(代碼中有體現(xiàn)),萬條數(shù)據(jù)不到一秒吧。

用SQlite建立一個簡單學生管理數(shù)據(jù)庫

數(shù)據(jù)庫中有兩個表一個是classstudent

Alt

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

Alt

student表結(jié)果

Alt

創(chuàng)建工程

我的工程如下:

Alt

直接上代碼(看注釋更通透)

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_H

student.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)文章

最新評論

运城市| 丰顺县| 东宁县| 保定市| 宣威市| 车致| 墨脱县| 闸北区| 阳春市| 会理县| 屏东市| 五原县| 西乌珠穆沁旗| 章丘市| 兴宁市| 五华县| 孝感市| 桐柏县| 定陶县| 南平市| 长丰县| 临泉县| 广东省| 沙洋县| 全椒县| 五莲县| 泸州市| 梅州市| 方正县| 津南区| 金塔县| 雅江县| 西林县| 惠安县| 轮台县| 昂仁县| 龙江县| 彭山县| 鸡泽县| 西城区| 虎林市|