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. QSqlQuery, MariaDB, error in Insert but I can't see it.
QtWS25 Last Chance

QSqlQuery, MariaDB, error in Insert but I can't see it.

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 274 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 16 Jul 2020, 14:56 last edited by
    #1

    I have written a C++ and this uses the QJSEngine to allow me to write JavaScript and then run the scripts without recompiling the C++.

    I've written a series of API routines in C++ that I expose to the JavaScript. I'm currently working on the database API and there is a problem with the demo insert, this is the text:

    INSERT INTO `tblTest` (`dtWhen`, `tmWhen`) VALUES (?,?)
    

    The parameters to along with this are:

    ["2020/7/16", "15:43:52"]
    

    I sent this to the C++ as a JSON object:

         {"op":"insert"
        ,"sql":"INSERT INTO `tblTest` (`dtWhen`, `tmWhen`) VALUES (?,?)"
     ,"fields":["biPri", "dtWhen", "tmWhen"]
     ,"params":[ds, tm]
    ,"success":"onRowResults"
    ,"failure":"onFailure"}
    

    This is part of the C++ that processes the JSON and performs the query:

    QString strSQL = itrSQL->toString();
    QSqlQuery query(strSQL);
    
    if ( itrParams != objDB.end() ) {
        QJsonArray aryParams = itrParams->toArray();
    
        for( int i=0; i<aryParams.count(); i++ )  {
            QVariant varParam(aryParams.at(i).toVariant());
            query.addBindValue(varParam);
        }
    }
    QSqlError err = query.lastError();
    

    The error I'm seeing is:

    INSERT INTO `tblTest` (`dtWhen`, `tmWhen`) VALUES (?,?)
    You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?,?)' at line 1 QMYSQL: Unable to execute query
    

    As far as I can see there is nothing wrong with the syntax and the parameters are valid, this is the table structure:

    CREATE TABLE `tblTest` (
      `biPri` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `dtWhen` date DEFAULT NULL,
      `tmWhen` time DEFAULT NULL,
      PRIMARY KEY (`biPri`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    

    Kind Regards,
    Sy

    S 1 Reply Last reply 16 Jul 2020, 15:06
    0
    • S SPlatten
      16 Jul 2020, 14:56

      I have written a C++ and this uses the QJSEngine to allow me to write JavaScript and then run the scripts without recompiling the C++.

      I've written a series of API routines in C++ that I expose to the JavaScript. I'm currently working on the database API and there is a problem with the demo insert, this is the text:

      INSERT INTO `tblTest` (`dtWhen`, `tmWhen`) VALUES (?,?)
      

      The parameters to along with this are:

      ["2020/7/16", "15:43:52"]
      

      I sent this to the C++ as a JSON object:

           {"op":"insert"
          ,"sql":"INSERT INTO `tblTest` (`dtWhen`, `tmWhen`) VALUES (?,?)"
       ,"fields":["biPri", "dtWhen", "tmWhen"]
       ,"params":[ds, tm]
      ,"success":"onRowResults"
      ,"failure":"onFailure"}
      

      This is part of the C++ that processes the JSON and performs the query:

      QString strSQL = itrSQL->toString();
      QSqlQuery query(strSQL);
      
      if ( itrParams != objDB.end() ) {
          QJsonArray aryParams = itrParams->toArray();
      
          for( int i=0; i<aryParams.count(); i++ )  {
              QVariant varParam(aryParams.at(i).toVariant());
              query.addBindValue(varParam);
          }
      }
      QSqlError err = query.lastError();
      

      The error I'm seeing is:

      INSERT INTO `tblTest` (`dtWhen`, `tmWhen`) VALUES (?,?)
      You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?,?)' at line 1 QMYSQL: Unable to execute query
      

      As far as I can see there is nothing wrong with the syntax and the parameters are valid, this is the table structure:

      CREATE TABLE `tblTest` (
        `biPri` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
        `dtWhen` date DEFAULT NULL,
        `tmWhen` time DEFAULT NULL,
        PRIMARY KEY (`biPri`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
      
      S Offline
      S Offline
      SPlatten
      wrote on 16 Jul 2020, 15:06 last edited by
      #2

      Actually, just spotted the error after I posted the correct code is:

          QString strSQL = itrSQL->toString();
          QSqlQuery query;
          query.prepare(strSQL);
      
          if ( itrParams != objDB.end() ) {
              QJsonArray aryParams = itrParams->toArray();
      
              for( int i=0; i<aryParams.count(); i++ )  {
                  QVariant varParam(aryParams.at(i).toVariant());
                  query.addBindValue(varParam);
              }
          }
          query.exec();
      

      Kind Regards,
      Sy

      1 Reply Last reply
      0

      1/2

      16 Jul 2020, 14:56

      • Login

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