try this example, works fine for me.
#include <QtGui>
#include <QtSql>
#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");
query.prepare("INSERT INTO pictures (id, pic) VALUES (?, ?)");
query.addBindValue(1);
query.addBindValue(byteArray);
qDebug() << query.exec();
qDebug() << query.exec("SELECT pic FROM pictures WHERE id = 1");
qDebug() << query.next();
byteArray = query.value(0).toByteArray();
qDebug() << pixmap.size();
label.setPixmap(pixmap);
label.show();
return app.exec();
}
#include <QtGui>
#include <QtSql>
#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");
query.prepare("INSERT INTO pictures (id, pic) VALUES (?, ?)");
query.addBindValue(1);
query.addBindValue(byteArray);
qDebug() << query.exec();
qDebug() << query.exec("SELECT pic FROM pictures WHERE id = 1");
qDebug() << query.next();
byteArray = query.value(0).toByteArray();
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