Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SQLite WHERE clause with BLOB. Or how to make QT query BLOB as x'00000000'?
QtWS25 Last Chance

SQLite WHERE clause with BLOB. Or how to make QT query BLOB as x'00000000'?

Scheduled Pinned Locked Moved Solved General and Desktop
sqlqueryqsqlquerysqlite
6 Posts 3 Posters 6.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kofr
    wrote on last edited by Kofr
    #1

    CASE:
    row is INTEGER SQL type
    id is BLOB SQL type

    I want to make this
    SELECT row FROM 'table' WHERE id = x'00000007'
    this works fine with SQLite browser app and my db.
    But following qt does not work and returns no items"

    QString queryContent = "SELECT row FROM \'" + tableName + "\' WHERE id = :id";
    QSqlQuery query;
    query.prepare(queryContent);
    query.bindValue(":id", t_id);
    

    comment: t_id is QByteArray

    How to fix Qt implementation?

    jsulmJ 1 Reply Last reply
    0
    • K Kofr

      CASE:
      row is INTEGER SQL type
      id is BLOB SQL type

      I want to make this
      SELECT row FROM 'table' WHERE id = x'00000007'
      this works fine with SQLite browser app and my db.
      But following qt does not work and returns no items"

      QString queryContent = "SELECT row FROM \'" + tableName + "\' WHERE id = :id";
      QSqlQuery query;
      query.prepare(queryContent);
      query.bindValue(":id", t_id);
      

      comment: t_id is QByteArray

      How to fix Qt implementation?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Kofr http://doc.qt.io/qt-5/qbytearray.html#toHex

      QString queryContent = "SELECT row FROM \'" + tableName + "\' WHERE id = x':id'";
      QSqlQuery query;
      query.prepare(queryContent);
      query.bindValue(":id", t_id.toHex());
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • K Offline
        K Offline
        Kofr
        wrote on last edited by Kofr
        #3

        It converts to HEX correctly but query fails
        QSqlQuery::value: not positioned on a valid record
        does not work neither

        QString queryContent = "SELECT row FROM \'" + QString::number(parentInt) + "\' WHERE id = x\':id\'";
            QSqlQuery query;
            query.prepare(queryContent);
            QByteArray idBA = t_id.toHex();
            qDebug() << idBA;
            query.bindValue(":id", idBA);
        

        and

        QString queryContent = "SELECT row FROM \'" + QString::number(parentInt) + "\' WHERE id = x':id'";
        ...
        
        jsulmJ 1 Reply Last reply
        0
        • K Kofr

          It converts to HEX correctly but query fails
          QSqlQuery::value: not positioned on a valid record
          does not work neither

          QString queryContent = "SELECT row FROM \'" + QString::number(parentInt) + "\' WHERE id = x\':id\'";
              QSqlQuery query;
              query.prepare(queryContent);
              QByteArray idBA = t_id.toHex();
              qDebug() << idBA;
              query.bindValue(":id", idBA);
          

          and

          QString queryContent = "SELECT row FROM \'" + QString::number(parentInt) + "\' WHERE id = x':id'";
          ...
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Kofr said in SQLite WHERE clause with BLOB. Or how to make QT query BLOB as x'00000000'?:

          QSqlQuery

          What does http://doc.qt.io/qt-5/qsqlquery.html#lastQuery return after you try to execute it?
          Is there a row with the ID you're trying to retrieve?
          Also I'm wondering what you are doing here:

          "SELECT row FROM \'" + QString::number(parentInt) + "\' WHERE id = x\':id\'";
          

          Is the name of the table a number?!
          The table name should be bound as well.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          K 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Kofr said in SQLite WHERE clause with BLOB. Or how to make QT query BLOB as x'00000000'?:

            QSqlQuery

            What does http://doc.qt.io/qt-5/qsqlquery.html#lastQuery return after you try to execute it?
            Is there a row with the ID you're trying to retrieve?
            Also I'm wondering what you are doing here:

            "SELECT row FROM \'" + QString::number(parentInt) + "\' WHERE id = x\':id\'";
            

            Is the name of the table a number?!
            The table name should be bound as well.

            K Offline
            K Offline
            Kofr
            wrote on last edited by
            #5

            @jsulm your first answer is not right as Qt does not care what to bind Hex or byte array I think.

            solution which works for me is

                QString queryContent = "SELECT row FROM \'" + QString::number(parentInt) + "\' WHERE id = x\'" + QString::fromLatin1(idBA) + "\'";
            
            

            Instead of binding, making a string.

            kshegunovK 1 Reply Last reply
            0
            • K Kofr

              @jsulm your first answer is not right as Qt does not care what to bind Hex or byte array I think.

              solution which works for me is

                  QString queryContent = "SELECT row FROM \'" + QString::number(parentInt) + "\' WHERE id = x\'" + QString::fromLatin1(idBA) + "\'";
              
              

              Instead of binding, making a string.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @Kofr
              If the above snippet works, then you don't respect the SQL's type. You're trying to pass a string representation to Qt when it clearly expects a binary (BLOB is "Binary Large OBject"). So then the question:

              How to fix Qt implementation?

              comes back as: "How to fix your implementation".
              Anyway, I'd suggest passing the appropriate type to the driver. My thoughts are that something like this should be working:

              const char rawData[4] = { 0x00, 0x00, 0x00, 0x07 };
              QByteArray id = QByteArray::fromRawData(rawData, 4);
              
              QSqlQuery query;
              if (!query.prepare(QStringLiteral("SELECT row FROM %1 WHERE id = :id").arg(tableName)))
                  // Handle the error!
              
              query.bindValue(":id", id); // Pass binary data for columns that expect binary
              
              if (!query.exec())
                  // Handle error again!
              

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved