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. how to insert a QJsonobject into a mysql field
Forum Updated to NodeBB v4.3 + New Features

how to insert a QJsonobject into a mysql field

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.1k 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.
  • C Offline
    C Offline
    cawlfj
    wrote on last edited by cawlfj
    #1

    I have a mysql table contains json field , and i define JSonobject and JSsonArray
    QString jjj="sdfd";

            QJsonObject JSSS;
    
            JSSS.insert("TypeID","IMO");
    
            JSSS.insert("Number","9992873");
    
            qDebug()<<JSSS;
    
            queryCdbN.prepare(" insert into ttt (test,imo) values(:test,:imo)");
    
            queryCdbN.bindValue(":test",jjj);
    
            queryCdbN.bindValue(":imo",JSSS);
    
            queryCdbN.exec();
    
    
    
            queryCdbN.prepare(" insert into ttt (test,imo) values(:test,JSON_OBJECT(:imo)");
    
            queryCdbN.bindValue(":test",jjj);
    
            queryCdbN.bindValue(":imo",JSSS);
    
            queryCdbN.exec();
    

    i get the output as below:
    QJsonObject({"Number":"9992873","TypeID":"IMO"})

    QSqlError("1064", "QMYSQL: Unable to execute query", "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1")
    

    seems the QJsonObject is empty?

    how to bindValue and insert into mysql json field?

    eyllanescE 1 Reply Last reply
    0
    • C cawlfj

      I have a mysql table contains json field , and i define JSonobject and JSsonArray
      QString jjj="sdfd";

              QJsonObject JSSS;
      
              JSSS.insert("TypeID","IMO");
      
              JSSS.insert("Number","9992873");
      
              qDebug()<<JSSS;
      
              queryCdbN.prepare(" insert into ttt (test,imo) values(:test,:imo)");
      
              queryCdbN.bindValue(":test",jjj);
      
              queryCdbN.bindValue(":imo",JSSS);
      
              queryCdbN.exec();
      
      
      
              queryCdbN.prepare(" insert into ttt (test,imo) values(:test,JSON_OBJECT(:imo)");
      
              queryCdbN.bindValue(":test",jjj);
      
              queryCdbN.bindValue(":imo",JSSS);
      
              queryCdbN.exec();
      

      i get the output as below:
      QJsonObject({"Number":"9992873","TypeID":"IMO"})

      QSqlError("1064", "QMYSQL: Unable to execute query", "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1")
      

      seems the QJsonObject is empty?

      how to bindValue and insert into mysql json field?

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @cawlfj try with: queryCdbN.bindValue(":imo", QJsonDocument(JSSS).toJson());

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      C 2 Replies Last reply
      2
      • eyllanescE eyllanesc

        @cawlfj try with: queryCdbN.bindValue(":imo", QJsonDocument(JSSS).toJson());

        C Offline
        C Offline
        cawlfj
        wrote on last edited by
        #3

        @eyllanesc said in how to insert a QJsonobject into a mysql field:

        QJsonDocument(JSSS).toJson()

        @eyllanesc change to queryCdbN.bindValue(":imo", QJsonDocument(JSSS).toJson());
        i got
        QSqlError("3144", "QMYSQL3: Unable to execute statement", "Cannot create a JSON value from a string with CHARACTER SET 'binary'.")

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cawlfj
          wrote on last edited by
          #4

          i updated mysql connector to 8.023

          QString jjj="sdfd";
          QJsonObject JSSS;
          JSSS.insert("TypeID","IMO");
          JSSS.insert("Number","9992873");
          qDebug()<<JSSS;
          queryCdbN.prepare(" insert into ttt (test,imo) values(:test,:imo)");
          queryCdbN.bindValue(":test",jjj);
          queryCdbN.bindValue(":imo",JSSS);
          queryCdbN.exec();
          qDebug()<<queryCdbN.lastQuery();
          qDebug()<<queryCdbN.lastError();

          will get

          QJsonObject({"Number":"9992873","TypeID":"IMO"})
          " insert into ttt (test,imo) values(:test,:imo)"
          QSqlError("3140", "QMYSQL3: Unable to execute statement", "Invalid JSON text: "The document is empty." at position 0 in value for column 'ttt.imo'.")

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What column type is imo?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            C 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              What column type is imo?

              C Offline
              C Offline
              cawlfj
              wrote on last edited by
              #6

              @Christian-Ehrlicher Json

              Christian EhrlicherC 1 Reply Last reply
              0
              • eyllanescE eyllanesc

                @cawlfj try with: queryCdbN.bindValue(":imo", QJsonDocument(JSSS).toJson());

                C Offline
                C Offline
                cawlfj
                wrote on last edited by
                #7

                @eyllanesc

                queryCdbN.prepare(" insert into ttt (test,imo) values(:test,CONVERT(:imo USING UTF8MB4) )");

                Now it is ok

                Many thanks

                1 Reply Last reply
                0
                • C cawlfj

                  @Christian-Ehrlicher Json

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by Christian Ehrlicher
                  #8

                  @cawlfj said in how to insert a QJsonobject into a mysql field:

                  Json

                  So it's basically a string - QString::fromUtf8(QJsonDocument(JSSS).toJson())

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2

                  • Login

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