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

简介: 如何在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


相关文章
|
2月前
|
关系型数据库 MySQL 数据库
Python处理数据库:MySQL与SQLite详解 | python小知识
本文详细介绍了如何使用Python操作MySQL和SQLite数据库,包括安装必要的库、连接数据库、执行增删改查等基本操作,适合初学者快速上手。
377 15
|
3月前
|
存储 SQL 数据库
数据库知识:了解SQLite或其他移动端数据库的使用
【10月更文挑战第22天】本文介绍了SQLite在移动应用开发中的应用,包括其优势、如何在Android中集成SQLite、基本的数据库操作(增删改查)、并发访问和事务处理等。通过示例代码,帮助开发者更好地理解和使用SQLite。此外,还提到了其他移动端数据库的选择。
71 8
|
4月前
|
Web App开发 SQL 数据库
使用 Python 解析火狐浏览器的 SQLite3 数据库
本文介绍如何使用 Python 解析火狐浏览器的 SQLite3 数据库,包括书签、历史记录和下载记录等。通过安装 Python 和 SQLite3,定位火狐数据库文件路径,编写 Python 脚本连接数据库并执行 SQL 查询,最终输出最近访问的网站历史记录。
72 4
|
4月前
|
存储 关系型数据库 数据库
轻量级数据库的利器:Python 及其内置 SQLite 简介
轻量级数据库的利器:Python 及其内置 SQLite 简介
101 3
|
4月前
|
应用服务中间件 PHP Apache
PbootCMS提示错误信息“未检测到您服务器环境的sqlite3数据库扩展...”
PbootCMS提示错误信息“未检测到您服务器环境的sqlite3数据库扩展...”
|
5月前
|
存储 API 数据库
QML使用Sqlite数据库存储ListModel数据
本文介绍了在QML中使用Sqlite数据库存储ListModel数据的方法,包括如何创建数据库、读取数据、动态添加和删除数据,以及如何在程序启动和退出时与数据库同步数据。
124 2
|
5月前
|
数据库 数据库管理
qt对sqlite数据库多线程的操作
本文总结了在Qt中进行SQLite数据库多线程操作时应注意的四个关键问题,包括数据库驱动加载、加锁、数据库的打开与关闭,以及QsqlQuery变量的使用。
310 1
|
6月前
|
人工智能 小程序 Java
【工具】轻松解锁SQLite数据库,一窥微信聊天记录小秘密
本文介绍了一款名为PyWxDump的开源工具,它可以获取微信账户信息、解密SQLite数据库以查看和备份聊天记录。此工具适用于已登录电脑版微信的用户,通过GitHub下载后简单几步即可操作。适合对数据恢复感兴趣的开发者,但请注意合法合规使用并尊重隐私。
819 2
【工具】轻松解锁SQLite数据库,一窥微信聊天记录小秘密
|
4月前
|
存储 缓存 关系型数据库
sqlite 数据库 介绍
sqlite 数据库 介绍
87 0
|
6月前
|
关系型数据库 Java MySQL
C#winform中使用SQLite数据库
C#winform中使用SQLite数据库
245 3
C#winform中使用SQLite数据库

热门文章

最新文章