欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Qt實(shí)現(xiàn)將BMP圖像數(shù)據(jù)存入SQLite數(shù)據(jù)庫

 更新時(shí)間:2025年06月26日 09:46:32   作者:XXYBMOOO  
這篇文章主要為大家詳細(xì)介紹了如何使用 Qt 框架實(shí)現(xiàn)了將 BMP 圖像文件以二進(jìn)制形式存入 SQLite 數(shù)據(jù)庫,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

本項(xiàng)目通過 Qt 框架實(shí)現(xiàn)了將 BMP 圖像文件以二進(jìn)制形式存入 SQLite 數(shù)據(jù)庫,并可從數(shù)據(jù)庫中讀取還原為 BMP 圖像文件的功能,適用于需要圖像與結(jié)構(gòu)化數(shù)據(jù)統(tǒng)一管理的場景。

整個(gè)流程分為兩個(gè)主要部分:

一、圖像保存到數(shù)據(jù)庫

程序首先連接指定的 SQLite 數(shù)據(jù)庫,若數(shù)據(jù)庫文件不存在將自動創(chuàng)建。在讀取本地 BMP 圖像文件后,將其內(nèi)容作為二進(jìn)制數(shù)據(jù)(BLOB)保存至數(shù)據(jù)庫中的一張名為 Images 的數(shù)據(jù)表中。表結(jié)構(gòu)簡單,僅包含自增主鍵 id 和存儲圖像數(shù)據(jù)的 data 字段。若數(shù)據(jù)表尚未存在,程序會自動創(chuàng)建。

通過這種方式,圖像內(nèi)容不再依賴文件系統(tǒng)存儲,而是與應(yīng)用數(shù)據(jù)一同保存在數(shù)據(jù)庫中,有利于集中管理、版本控制與數(shù)據(jù)傳輸。

二、從數(shù)據(jù)庫讀取圖像并保存為文件

讀取部分通過指定圖像的 id 值從數(shù)據(jù)庫中查詢對應(yīng)的 BLOB 數(shù)據(jù),并將其以 BMP 格式寫入本地文件。程序在執(zhí)行過程中提供異常判斷,例如數(shù)據(jù)庫連接失敗、圖像記錄不存在、文件寫入失敗等,確保操作的穩(wěn)定性和可追蹤性。

三、應(yīng)用場景與擴(kuò)展性

該方案適用于圖像歸檔、數(shù)字資源管理、數(shù)據(jù)庫驅(qū)動的圖像瀏覽系統(tǒng)等場景。雖然本文示例以 BMP 圖像為主,但該方法同樣適用于任意格式的二進(jìn)制文件,如 PNG、JPG、PDF 等,只需調(diào)整文件處理部分即可。此外,數(shù)據(jù)庫結(jié)構(gòu)可擴(kuò)展,如添加圖像名稱、分類標(biāo)簽、時(shí)間戳等字段,實(shí)現(xiàn)更復(fù)雜的圖像管理系統(tǒng)。

本項(xiàng)目展示了如何將 Qt 與 SQLite 結(jié)合,實(shí)現(xiàn)圖像與數(shù)據(jù)庫之間的數(shù)據(jù)交互,具有良好的通用性與可拓展性,為相關(guān)應(yīng)用開發(fā)提供了一種簡潔實(shí)用的實(shí)現(xiàn)思路。

#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QFile>
#include <QDebug>

void saveBmpToDatabase(const QString &imagePath, const QString &databasePath)
 {

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(databasePath);

    if (!db.open()) 
    {
        qDebug() << "無法打開數(shù)據(jù)庫";
        return;
    }

    // 讀取BMP圖像文件
    QFile file(imagePath);
    if (!file.open(QIODevice::ReadOnly)) 
    {
        qDebug() << "無法打開圖像文件";
        return;
    }

    QByteArray imageData = file.readAll();
    file.close();

    QSqlQuery query;
    query.exec("CREATE TABLE IF NOT EXISTS Images (id INTEGER PRIMARY KEY, data BLOB)");

    query.prepare("INSERT INTO Images (data) VALUES (:data)");
    query.bindValue(":data", imageData);

    if (!query.exec())
    {
        qDebug() << "插入圖像數(shù)據(jù)失敗";
    } else 
    {
        qDebug() << "圖像數(shù)據(jù)插入成功";
    }

    db.close();
}

int main(int argc, char *argv[]) 
{
    QCoreApplication a(argc, argv);

    QString imagePath = "a.bmp";
    QString databasePath = "image.db";

    saveBmpToDatabase(imagePath, databasePath);

    return a.exec();
}
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QFile>
#include <QDebug>

void loadBmpFromDatabase(const QString &outputPath, const QString &databasePath, int imageId)
 {

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(databasePath);

    if (!db.open()) 
    {
        qDebug() << "無法打開數(shù)據(jù)庫";
        return;
    }

    QSqlQuery query;
    query.prepare("SELECT data FROM Images WHERE id = :id");
    query.bindValue(":id", imageId);

    if (!query.exec()) 
    {
        qDebug() << "讀取圖像數(shù)據(jù)失敗";
        return;
    }

    if (query.next())
     {
        QByteArray imageData = query.value(0).toByteArray();

        QFile file(outputPath);
        if (!file.open(QIODevice::WriteOnly)) {
            qDebug() << "無法打開輸出文件";
            return;
        }

        file.write(imageData);
        file.close();

        qDebug() << "圖像已保存到" << outputPath;
    } else {
        qDebug() << "未找到指定的圖像";
    }

    db.close();
}

int main(int argc, char *argv[]) 
{
    QCoreApplication a(argc, argv);

    QString outputPath = "b.bmp";
    QString databasePath = "image.db";
    int imageId = 1; 

    loadBmpFromDatabase(outputPath, databasePath, imageId);

    return a.exec();
}

 到此這篇關(guān)于基于Qt實(shí)現(xiàn)將BMP圖像數(shù)據(jù)存入SQLite數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)Qt圖像數(shù)據(jù)存入SQLite內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論