Load Image From Db Problem
-
Hi
I have saved a pic to my Sql Server Db
but When i want To load it in my Graphic View
DO nothing :|
this is my code:
.h fileQPixmap originalPixmap; QPixmap outPixmap; QByteArray inByteArraye; QByteArray outByteArray;
.cpp file
readSkeletonPicQry.exec("SELECT SskeletonPic FROM Patient_File WHERE SmeliCode='"+seMcode+"'"); outByteArray = readSkeletonPicQry.value(0).toByteArray(); outPixmap = QPixmap(); outPixmap.loadFromData(outByteArray,"PNG"); scene->addPixmap(outPixmap); ui->graphicsView_2->setScene(scene)
my datatype in sql server Table in IMAGE and its store pics Fine
but i cant Show pic in graphic View From Table
*My Table Pic
whats Wrong?wrote on 25 Aug 2016, 13:13 last edited by M4RZB4Ni@M4RZB4Ni
this is all of my codes:
see it and if you can correct it
.h file:QImage *pix; QImage originalPixmap; QPixmap outPixmap; QPixmap pxp; QGraphicsPixmapItem pixItem; QByteArray inByteArraye; QByteArray outByteArray;
.cpp file:
void Medical_Records::shootScreen() { QScreen *screen = QGuiApplication::primaryScreen(); if (const QWindow *window = windowHandle()) screen = window->screen(); if (!screen) return; originalPixmap=ui->graphicsView_2->grab().toImage(); updateGraphicView(); } void Medical_Records::initalizeVarables() { shootScreen(); QBuffer inBuffer( &inByteArraye ); inBuffer.open(QIODevice::WriteOnly); originalPixmap.save(&inBuffer,"PNG"); } void Medical_Records::readSkeletonPic() { moveToThread(readSkeletonPicThread); readSkeletonPicQry.exec("SELECT SskeletonPic FROM Patient_File WHERE SmeliCode='"+seMcode+"'"); readSkeletonPicQry.next(); outByteArray = readSkeletonPicQry.value(0).toByteArray(); //outPixmap = QPixmap(); originalPixmap.loadFromData(outByteArray,"PNG"); qDebug() << "outByteArray :" << outByteArray.size(); qDebug() << "outByteArray :" << originalPixmap.size(); outPixmap.fromImage(originalPixmap.fromData(outByteArray,"PNG")); scene->addPixmap(outPixmap); ui->graphicsView_2->setScene(scene); qDebug() << readSkeletonPicQry.exec(); readSkeletonPicThread->start(); }
-
wrote on 25 Aug 2016, 13:20 last edited by
i don't get the threading thing but it's probably not the point here. where did you get the
"Cant Convert varchar datatype to VARBINARY(MAX)" error? -
i don't get the threading thing but it's probably not the point here. where did you get the
"Cant Convert varchar datatype to VARBINARY(MAX)" error? -
wrote on 25 Aug 2016, 13:59 last edited by
Can you post that code please?
-
wrote on 25 Aug 2016, 14:07 last edited by
@VRonin
yes of course
thanks so muchthis is code :
void Medical_Records::initalizeVarables() { shootScreen(); QBuffer inBuffer( &inByteArraye ); inBuffer.open(QIODevice::WriteOnly); originalPixmap.save(&inBuffer,"PNG"); } void Medical_Records::submitWithScreenShot() { initalizeVarables(); QSqlQuery sql; sql.exec("UPDATE Patient_File SET SskeletonPic='"+inByteArraye+"' WHERE SmeliCode='"+seMcode+"';"); }
-
wrote on 25 Aug 2016, 16:49 last edited by VRonin
yep, you should check QSqlQuery::bindValue
sql.prepare("UPDATE Patient_File SET SskeletonPic= :skp WHERE SmeliCode= :smc"); sql.bindValue(":skp",inByteArraye); sql.bindValue(":smc",seMcode); sql.exec();
this also prevents SQL Injection. You should never really use unescaped input directly to build the query string
-
yep, you should check QSqlQuery::bindValue
sql.prepare("UPDATE Patient_File SET SskeletonPic= :skp WHERE SmeliCode= :smc"); sql.bindValue(":skp",inByteArraye); sql.bindValue(":smc",seMcode); sql.exec();
this also prevents SQL Injection. You should never really use unescaped input directly to build the query string
-
wrote on 25 Aug 2016, 16:58 last edited by VRonin
make the field varbinary(max), save it that way and the code you have will work, no changes needed on the load part
image in SQL server is a fixed length string. it has nothing to do with images
-
wrote on 21 Jun 2017, 14:48 last edited by
Hi!
The same problem, you could solve?
Thanks -
Summary of the above:
- in the database use the type
VABBINARY
notIMAGE
- use QSqlQuery::bindValue instead of string concatenation to build your query
wrote on 22 Jun 2017, 06:00 last edited by@VRonin Thank you for reply, but my problem is the load image from database, please see my another question:
QT 5.8 image from database error - in the database use the type