PDA

View Full Version : QSqlQuery Insert BLOB, But Not all the BLOB Is Inserted?



zerokewl
10th September 2013, 08:01
Hello,

I am inserting Image Data from a QNetworkReply into a MySQL Table.
It inserts most records fine, but some have corrupted BLOB data. I am inserting images from a QNetworkReply.

When i check the images in the Database, i find some are corrupted, and the bottom parts of the image seem to having missing/corrupt data, so it looks like part of the image was corrupted, or it didn't finish inserting the BLOB data in the query.

I can confirm all the data is received via the QNetworkReply because i create QLabels and Insert the Data into a QPixmap to view what it had downloaded before it inserts the data into the database.
I should mention, this QNetworkReply *reply, is received from the QNetworkAccessManager SIGNAL finished(QNetworkReply*).


QByteArray imageData = reply->readAll();
QLabel *label = new QLabel();
QPixmap pixmap;
pixmap.loadFromData(imageData);
label->setPixmap(pixmap);
label->show();

Then i insert the data to the database


QSqlQuery query(db);
query.prepare("INSERT INTO altimages (productId, data) VALUES (:productId, :data);");
query.bindValue(":productId", productId);
query.bindValue(":data", imageData);
if (!query.exec()){
qDebug() << "Error Inserting Data: " << query.lastError() << endl;
}

The last image inserted is corrupted, and also some other inserts in the middle of the insert queue, so i think that the query is terminated or destroyed and the QSqlQuery is not completely executed?

Here is the resulting image: 9554

Am i missing something ? Is the query not finishing properly?

Thank you.


Edit: I just checked also if the Bound Values of the Query were correct by creating QLabels and QPixmaps and seeing what the data was executed, and everytime, the pictures are ok and not corrupted.


QLabel *label = new QLabel();
QPixmap pixmap;
pixmap.loadFromData(query.boundValue(":data").toByteArray());
label->setPixmap(pixmap);
label->show();

I wonder why still in the database I'm seeing the Values of the BLOB with Corrupted or what looks more like incomplete blob data images. I'm using MYSQL Workbench to view the file in the database.

Edit: Just found this (http://stackoverflow.com/questions/8657086/qt-qsqlquery-binary-data-is-interpreted-as-string-when-binding-to-blob-field)on Stack OverFlow, and wondering if this is the reason.

Added after 16 minutes:

Sorry... this is kinda silly, but i thought BLOB was alot larger than 64kb...

BLOBs are restricted to 64KB, MEDIUMBLOBS to 16MB and LARGEBLOBS to 2GB

Changing that fixed it all.. Hope this helps someone save a few hours :-( /facepalm!