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

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(Add "cleanup" tag)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:snippets]]
[[Category:snippets]]
[[Category:HowTo]]
[[Category:HowTo]]

Revision as of 15:47, 3 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

Deutsch | English | Español | Български

Speichern und Abfragen eines Bildes / einer Datei mit SQLite

Bilder und jede andere Datei kann man in einer Datenbank speichern. Der folgende Code-Schnipsel zeigt beispielhaft, wie das mit ein paar Zeilen Code mit SQLite gemacht wird.

1. Die System-Datei in ein QByteArray einlesen 2. Das QByteArray als Binary Large Object (BLOB) in der Datenbank speichern

Beispiel :

 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();

Nun kann man das Bild / die Datei ganz normal abfragen:

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

Eine QPixmap aus einem QByteArray erzeugen:

 QPixmap pixmap = QPixmap();
 pixmap.loadFromData(array);