How to Store and Retrieve Image on SQLite: Difference between revisions
Jump to navigation
Jump to search
AutoSpider (talk | contribs) (Add "cleanup" tag) |
(Cleanup) |
||
Line 1: | Line 1: | ||
{{ | {{LangSwitch}} | ||
[[Category:snippets]] | [[Category:snippets]] | ||
[[Category:HowTo]] | [[Category:HowTo]] | ||
Images or any files can be stored in a database. Here is one way to do it using the following steps: | Images or any files can be stored in a database. Here is one way to do it using the following steps: | ||
# Read the system file into a QByteArray. | |||
# Store QByteArray as a Binary Large Object in database. | |||
For example : | For example : |
Revision as of 00:05, 28 June 2015
Images or any files can be stored in a database. Here is one way to do it using the following steps:
- Read the system file into a QByteArray.
- Store QByteArray as a Binary Large Object in database.
For example :
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();
Now, the image/file can be retrieved like any other data
QSqlQuery query("SELECT imgdata FROM imgtable");
query.next();
QByteArray array = query.value(0).toByteArray();
Creating a QPixmap from QByteArray :
QPixmap pixmap = QPixmap();
pixmap.loadFromData(array);
It is done. Now the pixmap can be used in a Button as icon or in label etc.