How to Store and Retrieve Image on SQLite: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:snippets]]<br />[[Category:HowTo]]
[[Category:snippets]]
[[Category:HowTo]]


'''English''' | [[How_to_Store_and_Retrieve_Image_on_SQLite_German|Deutsch]] | [[How_to_Store_and_Retrieve_an_Image_or_File_with_SQLite_Spanish|Español]] | [[How_to_Store_and_Retrieve_Image_on_SQLite_Bulgarian|Български]]
'''English''' | [[How_to_Store_and_Retrieve_Image_on_SQLite_German|Deutsch]] | [[How_to_Store_and_Retrieve_an_Image_or_File_with_SQLite_Spanish|Español]] | [[How_to_Store_and_Retrieve_Image_on_SQLite_Bulgarian|Български]]
Line 7: Line 8:
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:


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


For example :
For example :


<code><br /> QFile file&amp;amp;#40;fileName&amp;amp;#41;;<br /> if (!file.open(QIODevice::ReadOnly)) return;<br /> QByteArray byteArray = file.readAll();
<code>
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) return;
QByteArray byteArray = file.readAll();


QSqlQuery query;<br /> query.prepare(&quot;INSERT INTO imgtable (imgdata) VALUES (?)&quot;);<br /> query.addBindValue(byteArray);<br /> query.exec&amp;amp;#40;&amp;#41;;<br /></code>
QSqlQuery query;
query.prepare("INSERT INTO imgtable (imgdata) VALUES (?)");
query.addBindValue(byteArray);
query.exec();
</code>


Now, the image/file can be retrieved like any other data<br /><code><br /> QSqlQuery query(&quot;SELECT imgdata FROM imgtable&amp;quot;);<br /> query.next();<br /> QByteArray array = query.value(0).toByteArray();<br /></code>
Now, the image/file can be retrieved like any other data
<code>
QSqlQuery query("SELECT imgdata FROM imgtable");
query.next();
QByteArray array = query.value(0).toByteArray();
</code>


Creating a QPixmap from QByteArray :<br /><code><br /> QPixmap pixmap = QPixmap();<br /> pixmap.loadFromData(array);<br /></code>
Creating a QPixmap from QByteArray :
<code>
QPixmap pixmap = QPixmap();
pixmap.loadFromData(array);
</code>


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

Revision as of 09:08, 25 February 2015


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 :

 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.