How to Store and Retrieve Image on SQLite/de

From Qt Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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);