Important: Please read the Qt Code of Conduct -

Problem with QByteArray and BLOB Entry in Database

  • i have a Problem to find a Databaseentry with a QByteArray. i encrypt a QString and get a QByteArray that i can insert to my Database. The Databaseentry looks like this.


    but if i search for an entry my variable shows like this.


    so the Select query always returns nothing. Here my Code

    QSqlQuery query; 
        qDebug() << title; 
        query.prepare("SELECT situation_field, emotion_field, mind_field, strength FROM event " 
                      "WHERE title = CAST((:title) AS BLOB) AND date = (:date)"); 
        query.bindValue(":title", title)); 
        query.bindValue(":date", date.toString()); 
        if (query.exec()) 
            if (query.first()) 
                *situation = query.value(0).toByteArray(); 
                *emotion = query.value(1).toByteArray(); 
                *mind = query.value(2).toByteArray(); 
                *strength = query.value(3).toInt(); 

    Anyone has an idea what i can do? Nothing works.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you trying to show these values through qDebug() ?

  • Yes i do. I wanted to know why the query is empty, so i showed the variable with qdebug.

  • Lifetime Qt Champion

    To check what query was run your can call executedQuery.

    What you see in the console is explained in this email thread

    Out of curiosity why are situation, emotion, mind and strength pointers ?

  • i found out that my Values i want to search for are always different. The Values i insert into the Database are encrypted and everytime i encrypt a String, the QByteArray looks different at the end.

    This Variables are Pointers because the Code is placed in a Method of a Class and it would be to difficult to return them all.

  • Lifetime Qt Champion

    Do you mean that if you encrypt the same string twice you get two different results ?

  • Yes. But i finally solved my Problem. Its to complicated to explain, but thanks for your help.

Log in to reply