如何在Qt中使用数据库Sqlite保存和提取图片

先看Qt官方的例子:源码来自How to Store and Retrieve Image on SQLite
https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite

#include "mainwindow.h"

//源码来自
//How to Store and Retrieve Image on SQLite
//https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite
#include <QApplication>
#include <QtSql>
#include <QtWidgets>

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

    // Set up database
    QString dbName("myDatabase.db3"); //默认路径在文件夹build-sqldemo-Desktop_Qt_5_12_9_MSVC2017_64bit-Debug
    QFile::remove(dbName);            // delete sqlite file if it exists from a previous run
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(dbName);
    db.open();
    QSqlQuery query = QSqlQuery(db);
    query.exec("CREATE TABLE IF NOT EXISTS imgTable ( filename TEXT, imagedata BLOB )");

    // Generate an image (in this case a screenshot) and store it in a QByteArray
    QScreen *screen = a.primaryScreen();
    QPixmap inPixmap = screen->grabWindow(0);
    QByteArray inByteArray;
    QBuffer inBuffer(&inByteArray);
    inBuffer.open(QIODevice::WriteOnly);
    inPixmap.save(&inBuffer, "PNG"); // write inPixmap into inByteArray in PNG format

    // Alternatively, load an image file directly into a QByteArray
    // QFile file("screenshot.png");
    // if (!file.open(QIODevice::ReadOnly)) return;
    // QByteArray inByteArray = file.readAll();

    // Insert image bytes into the database
    // Note: make sure to wrap the :placeholder in parenthesis
    query.prepare("INSERT INTO imgTable (filename, imagedata) VALUES ('screenshot.png', :imageData)");
    query.bindValue(":imageData", inByteArray);
    if (!query.exec())
        qDebug() << "Error inserting image into table:\n"
                 << query.lastError();

    // Get image data back from database
    if (!query.exec("SELECT imagedata from imgTable"))
        qDebug() << "Error getting image from table:\n"
                 << query.lastError();
    query.first();
    QByteArray outByteArray = query.value(0).toByteArray();
    QPixmap outPixmap = QPixmap();
    outPixmap.loadFromData(outByteArray);
    db.close();

    // Display image
    QLabel myLabel;
    myLabel.setPixmap(outPixmap);
    myLabel.show();

    //MainWindow w;
    //w.show();
    return a.exec();
}

 

打开文件,读取二进制数据

    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) return;
    QByteArray byteArray = file.readAll();
 
    QSqlQuery query;
    query.prepare("INSERT INTO imgtable (imgdata) VALUES (?)");
    query.addBindValue(byteArray);
    query.exec();

 

打开多个数据库

QString connName = "basedb"

 QSqlDatabase db ;

 if(QSqlDatabase::contains(connName)){
        //如已经打开这个数据库,直接调出这个数据连接
      db = QSqlDatabase::database(connName);
    }else //否则打开这个数据库,注意带上数据库名
      db = QSqlDatabase::addDatabase("QSQLITE",connName);

 

//操作数据库1

QSqlQuery query;

//默认数据库1

query.exec("select * from student");

//操作数据库2

QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE","a");

//连接数据库2

QSqlQuery query(db2);

//绑定db2

query.exec("select * from teacher");

 

---

推荐阅读:

https://www.sqlite.org/index.html

http://qtdebug.com/qtbook-db-util/

http://qtdebug.com/qtbook-db-common/

http://www.qtcn.org/bbs/read-htm-tid-88649.html

https://gitee.com/feiyangqingyun/QWidgetDemo/tree/master/dbpage

https://blog.csdn.net/wsj18808050/article/details/44891715

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页