Going via QPixmap may result in the image being stored with different metadata or compressed differently when saved to the buffer... leading to a binary but not visible difference. Given that you already have the image in a file and you only wish to store it there really is no need to go via QPixmap and QBuffer at all.
This code works with a Mysql mediumblob. The in.png file and out.png file are bit-for-bit equal:
#include <QCoreApplication>
#include <QFile>
#include <QByteArray>
#include <QVariant>
#include <QSqlDatabase>
#include <QSqlQuery>
int main(int argc, char **argv)
{
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("test");
db.setPassword("test");
if (db.open()) {
qry.prepare("INSERT INTO testdata (firstname , secondname , userfoto) VALUES (?,?,?)");
qry.bindValue(2, data);
qry.exec();
}
qry.prepare("SELECT userfoto from testdata");
qry.exec();
qry.next();
out.write(data);
}
}
return 0;
}
#include <QCoreApplication>
#include <QFile>
#include <QByteArray>
#include <QVariant>
#include <QSqlDatabase>
#include <QSqlQuery>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("test");
db.setPassword("test");
if (db.open()) {
QSqlQuery qry("delete from testdata");
QFile in("in.png");
if (in.open(QIODevice::ReadOnly)) {
QByteArray data = in.readAll();
qry.prepare("INSERT INTO testdata (firstname , secondname , userfoto) VALUES (?,?,?)");
qry.bindValue(0, QVariant("Dummy"));
qry.bindValue(1, QVariant("Dummy"));
qry.bindValue(2, data);
qry.exec();
}
QFile out("out.png");
if (out.open(QIODevice::WriteOnly)) {
qry.prepare("SELECT userfoto from testdata");
qry.exec();
qry.next();
QByteArray data = qry.value(0).toByteArray();
out.write(data);
}
}
return 0;
}
To copy to clipboard, switch view to plain text mode
CREATE TABLE `testdata` (
`firstname` varchar(50) DEFAULT NULL,
`secondname` varchar(50) DEFAULT NULL,
`userfoto` mediumblob
)
CREATE TABLE `testdata` (
`firstname` varchar(50) DEFAULT NULL,
`secondname` varchar(50) DEFAULT NULL,
`userfoto` mediumblob
)
To copy to clipboard, switch view to plain text mode
Qt 5.5.1, Mysql 5.6.28, Linux
Bookmarks