How to Store and Retrieve Image on SQLite
Jump to navigation
Jump to search
English | Deutsch | Español | Български
How to Store and Retrieve an Image or File with SQLite
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 :
<br /> QFile file&amp;#40;fileName&amp;#41;;<br /> if (!file.open(QIODevice::ReadOnly)) return;<br /> QByteArray byteArray = file.readAll();
QSqlQuery query;<br /> query.prepare("INSERT INTO imgtable (imgdata) VALUES (?)");<br /> query.addBindValue(byteArray);<br /> query.exec&amp;#40;&#41;;<br />
Now, the image/file can be retrieved like any other data
<br /> QSqlQuery query("SELECT imgdata FROM imgtable&quot;);<br /> query.next();<br /> QByteArray array = query.value(0).toByteArray();<br />
Creating a QPixmap from QByteArray :
<br /> QPixmap pixmap = QPixmap();<br /> pixmap.loadFromData(array);<br />
It is done. Now the pixmap can be used in a Button as icon or in label etc.