take a look at this example, works fine
#include <QtGui>
#include <QtSql>
#include <QtXml>
#include <QApplication>
int main(int argc, char **argv)
{
db.setDatabaseName("pictures");
if (!db.open())
return db.lastError().number();
qDebug() << query.exec("CREATE TABLE IF NOT EXISTS pictures (id INT PRIMARY KEY, pic BLOB)");
qDebug() << image.size();
image.save(&buffer, "jpg");
doc.appendChild(root);
QDomText data
= doc.
createTextNode(byteArray.
toBase64());
root.appendChild(data);
query.prepare("INSERT INTO pictures (id, pic) VALUES (?, ?)");
query.addBindValue(1);
query.addBindValue(doc.toString());
qDebug() << query.exec();
qDebug() << query.exec("SELECT pic FROM pictures WHERE id = 1");
qDebug() << query.next();
doc.setContent(query.value(0).toString());
root = doc.documentElement();
data = root.firstChild().toText();
byteArray
= QByteArray::fromBase64(data.
toText().
data().
toAscii());
qDebug() << pixmap.size();
label.setPixmap(pixmap);
label.show();
return app.exec();
}
#include <QtGui>
#include <QtSql>
#include <QtXml>
#include <QApplication>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("pictures");
if (!db.open())
return db.lastError().number();
QSqlQuery query;
qDebug() << query.exec("CREATE TABLE IF NOT EXISTS pictures (id INT PRIMARY KEY, pic BLOB)");
QImage image("flower.jpg");
qDebug() << image.size();
QByteArray byteArray;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "jpg");
QDomDocument doc;
QDomElement root = doc.createElement("PictureData");
doc.appendChild(root);
QDomText data = doc.createTextNode(byteArray.toBase64());
root.appendChild(data);
query.prepare("INSERT INTO pictures (id, pic) VALUES (?, ?)");
query.addBindValue(1);
query.addBindValue(doc.toString());
qDebug() << query.exec();
qDebug() << query.exec("SELECT pic FROM pictures WHERE id = 1");
qDebug() << query.next();
doc.setContent(query.value(0).toString());
root = doc.documentElement();
data = root.firstChild().toText();
byteArray = QByteArray::fromBase64(data.toText().data().toAscii());
QPixmap pixmap = QPixmap::fromImage(QImage::fromData(byteArray, "jpg"));
qDebug() << pixmap.size();
QLabel label;
label.setPixmap(pixmap);
label.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks