How to Store and Retrieve Image on SQLite

From Qt Wiki
Revision as of 15:34, 16 November 2015 by Waldyrious (talk | contribs) (various minor fixes)

Jump to: navigation, search

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Images or any files can be stored in a database. Here is one way to do it using the following steps:

  1. Read the system file into a QByteArray.
  2. Store QByteArray as a Binary Large Object in database.

For example:

QFile file(fileName);
if (! return;
QByteArray byteArray = file.readAll();

QSqlQuery query;
query.prepare("INSERT INTO imgtable (imgdata) VALUES (?)");

Now, the image/file can be retrieved like any other data

QSqlQuery query("SELECT imgdata FROM imgtable");;
QByteArray array = query.value(0).toByteArray();

Creating a QPixmap from QByteArray :

 QPixmap pixmap = QPixmap();

It is done. Now the pixmap can be used in a QPushButton as icon or in label etc.