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:
void Dialog::RetrieveFromDatabase()
{
QString imagePath
= QFileDialog::getSaveFileName(this,
"Save image to",
QDir::currentPath(),
"JPEG Image (*.jpg)");
query.prepare("SELECT image FROM `test`.`images` WHERE `id_no`=':id';");
query.bindValue(":id",ui->lineID->text());
if (!query.exec())
{
QMessageBox::critical(this,
"Query error",query.
lastError().
text());
}
else
{
//Load image form database
QByteArray bytes
= query.
value(query.
record().
indexOf("image")).
toByteArray();
//Save image to disk
imageWrite.loadFromData(bytes,"JPG");
imageWrite.save(imagePath,"JPG");
}
}
void Dialog::RetrieveFromDatabase()
{
QString imagePath = QFileDialog::getSaveFileName(this,"Save image to",QDir::currentPath(),"JPEG Image (*.jpg)");
QSqlQuery query;
query.prepare("SELECT image FROM `test`.`images` WHERE `id_no`=':id';");
query.bindValue(":id",ui->lineID->text());
if (!query.exec())
{
QMessageBox::critical(this,"Query error",query.lastError().text());
}
else
{
//Load image form database
QByteArray bytes = query.value(query.record().indexOf("image")).toByteArray();
//Save image to disk
QImage imageWrite;
imageWrite.loadFromData(bytes,"JPG");
imageWrite.save(imagePath,"JPG");
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks