Important: Please read the Qt Code of Conduct -

Load Image From Db Problem

  • Any Body Cant Help me?

  • Lifetime Qt Champion

    Its impossible to guess why
    "SELECT SskeletonPic FROM Patient_File WHERE SmeliCode='"+seMcode+"'"
    is not correct.
    Could be non existing seMcode
    Anyway, you MUST use correct error handling to have a fighting chance
    so please add error checking.

    as in

    QSqlQuery query;
    query.exec(QString("select NAME from PEOPLE where AGE=%1").arg(age));
    if (
    qDebug() << "query ok:";
    } else {
        qDebug() << "SqLite error:" << query.lastError().text() << ", error code:" << query.lastError().number();

  • @mrjj
    seMcode is true because i can select more data with it !
    i think problem is converting from table(IMAGE DATATYPE) TO byte array !
    and i write your qdebug code and says query ok!

  • Lifetime Qt Champion

    well check return code from loadFromData to know if it fails.
    and the size of QBytearray

  • @mrjj
    According to qDebug Report

    outByteArray : 8
    QODBCResult::exec: Unable to execute statement: "[Microsoft][ODBC Driver Manager] Function sequence error"

    Byte array size is 8

  • Lifetime Qt Champion

    Ok so it seems it dont like your BLOB
    8 is not much for image so I wonder
    if it saved more than 8 and just the reading that failed.

    Anyway, its seems not to be Qt related as such so you should find out what
    ODBC + "Function sequence error" means.

    but there are many others.

  • @M4RZB4Ni said in Load Image From Db Problem:

    i think problem is converting from table(IMAGE DATATYPE) TO byte array !

    are you using image as field type in your database? if so you shouldn't, it's deprecated and will be removed (see use varbinary(max) instead.

    Also, you should use QImage instead of QPixmap.

    Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. Finally, the QPicture class is a paint device that records and replays QPainter commands.

  • @VRonin
    yes i use IMAGE because when i use VABBINARY(MAX) when i want save image
    program show error
    "Cant Convert varchar datatype to VARBINARY(MAX)!
    and i should Use IMAGE datatype!

  • How do you save it?

    You should definitely use VARBINARY:

    From Microsoft, page linked above:

    IMPORTANT! ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

  • @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)
    void Medical_Records::initalizeVarables()
            QBuffer inBuffer( &inByteArraye );
    void Medical_Records::readSkeletonPic()
      readSkeletonPicQry.exec("SELECT SskeletonPic FROM Patient_File WHERE SmeliCode='"+seMcode+"'");;
        outByteArray = readSkeletonPicQry.value(0).toByteArray();
        //outPixmap = QPixmap();
        qDebug() << "outByteArray :" << outByteArray.size();
        qDebug() << "outByteArray :" << originalPixmap.size();
        qDebug() << readSkeletonPicQry.exec();

  • 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?

  • @VRonin
    when i want insert image into database after capture screenshot

  • Can you post that code please?

  • @VRonin
    yes of course
    thanks so much

    this is code :

    void Medical_Records::initalizeVarables()
            QBuffer inBuffer( &inByteArraye );
    void Medical_Records::submitWithScreenShot()
        QSqlQuery sql;
        sql.exec("UPDATE Patient_File SET SskeletonPic='"+inByteArraye+"' WHERE SmeliCode='"+seMcode+"';");

  • yep, you should check QSqlQuery::bindValue

    sql.prepare("UPDATE Patient_File SET SskeletonPic= :skp  WHERE SmeliCode= :smc");

    this also prevents SQL Injection. You should never really use unescaped input directly to build the query string

  • @VRonin
    thanks but my problem is in Load image not save it!
    Thank a lot My Friend :)

  • 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

  • Hi!
    The same problem, you could solve?

  • @Kutyus

    Summary of the above:

    • in the database use the type VABBINARY not IMAGE
    • use QSqlQuery::bindValue instead of string concatenation to build your query

  • @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