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. QJsonArray populated but not written to database?
Forum Updated to NodeBB v4.3 + New Features

QJsonArray populated but not written to database?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 194 Views 1 Watching
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I have a table structure in MariaDB which one of the fields is using the JSON field type. I've used this before without any problem.

    I am reading binary data from a large file and writing it in 512 byte chunks to the database first converting to a QJsonArray.

    I can see in the debugger that the array is populated before writing and does contain 512 items.

    I am using stored procedures to avoid any syntax issues using prepared stored procedures. My procedure addBlock, which I call with 3 parameters:

    QSqlQuery queryBlock;
    queryBlock.prepare("CALL addBlock(?,?,?);");
    queryBlock.addBindValue(crstrFilename);
    queryBlock.addBindValue(muint16Block);
    queryBlock.addBindValue(aryData);
    Trainer::queryDB(queryBlock);
    

    crstrFilename is a const QString containing a fullpath and name of a file.
    muint16Block is an quint16 which is incremented on every call.
    aryData is an instance of QJsonArray which is populated and contains 512 items.
    queryDB is my function that performs the DB query and manages any errors, of which they're are none.

    As a result of calling this procedure I can see many records during the operation inserted and the block increments for each, but the JSON data appears to be empty.

    Why?

    Kind Regards,
    Sy

    KroMignonK 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I have a table structure in MariaDB which one of the fields is using the JSON field type. I've used this before without any problem.

      I am reading binary data from a large file and writing it in 512 byte chunks to the database first converting to a QJsonArray.

      I can see in the debugger that the array is populated before writing and does contain 512 items.

      I am using stored procedures to avoid any syntax issues using prepared stored procedures. My procedure addBlock, which I call with 3 parameters:

      QSqlQuery queryBlock;
      queryBlock.prepare("CALL addBlock(?,?,?);");
      queryBlock.addBindValue(crstrFilename);
      queryBlock.addBindValue(muint16Block);
      queryBlock.addBindValue(aryData);
      Trainer::queryDB(queryBlock);
      

      crstrFilename is a const QString containing a fullpath and name of a file.
      muint16Block is an quint16 which is incremented on every call.
      aryData is an instance of QJsonArray which is populated and contains 512 items.
      queryDB is my function that performs the DB query and manages any errors, of which they're are none.

      As a result of calling this procedure I can see many records during the operation inserted and the block increments for each, but the JSON data appears to be empty.

      Why?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @SPlatten said in QJsonArray populated but not written to database?:

      queryBlock.addBindValue(aryData);

      QJsonArray is not a valid type for QSqlQuery::addBindValue().
      I would suggest you to convert it to QString, this should work:

      QJsonDocument doc;
      doc.setArray(aryData);
      queryBlock.addBindValue(doc.toJson(QJsonDocument::Compact));
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      SPlattenS 1 Reply Last reply
      3
      • KroMignonK KroMignon

        @SPlatten said in QJsonArray populated but not written to database?:

        queryBlock.addBindValue(aryData);

        QJsonArray is not a valid type for QSqlQuery::addBindValue().
        I would suggest you to convert it to QString, this should work:

        QJsonDocument doc;
        doc.setArray(aryData);
        queryBlock.addBindValue(doc.toJson(QJsonDocument::Compact));
        
        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @KroMignon , thank you and thats exactly what I did elsewhere, just forgot.

        Kind Regards,
        Sy

        1 Reply Last reply
        0

        • Login

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