I want to read the BLOB field in my table to reconstruct the image save there my problem is that can't retrieve the value of BLOB from db to (bytes) which is (QByteArray).

This is my code:
Qt Code:
  1. void Dialog::RetrieveFromDatabase()
  2. {
  3. QString imagePath = QFileDialog::getSaveFileName(this,"Save image to",QDir::currentPath(),"JPEG Image (*.jpg)");
  4.  
  5. QSqlQuery query;
  6. query.prepare("SELECT image FROM `test`.`images` WHERE `id_no`=':id';");
  7. query.bindValue(":id",ui->lineID->text());
  8. if (!query.exec())
  9. {
  10. QMessageBox::critical(this,"Query error",query.lastError().text());
  11. }
  12. else
  13. {
  14. //Load image form database
  15. QByteArray bytes = query.value(query.record().indexOf("image")).toByteArray();
  16.  
  17. //Save image to disk
  18. QImage imageWrite;
  19. imageWrite.loadFromData(bytes,"JPG");
  20. imageWrite.save(imagePath,"JPG");
  21. }
  22. }
To copy to clipboard, switch view to plain text mode