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. Is QSqlQuery not support json type in mysql ?
Forum Updated to NodeBB v4.3 + New Features

Is QSqlQuery not support json type in mysql ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.6k 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.
  • F Offline
    F Offline
    funnydog
    wrote on last edited by
    #1

    I have a json type data in mysql, and when i retrieve the data like this:

        QSqlQuery   query;
        query.prepare("SELECT * from mytable");
        book ok query.exec(); // ok, always return true
        bool active   = query.isActive();// ok, always return true
        bool selected = query.isSelect();// ok, always return true
        bool valid= query.isValid();// error, always return false
        while (query.next())// error, query.next() always return false
        {
           // some logic code
        }
    

    I cannot retrieve any data. However, once I change the json type to TEXT in mysql, everything is ok and I can get the data.
    So, is QSqlQuery not support json type in mysql ?
    My Qt version is 5.12. Mysql version is 5.7.20.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      AFAIK, no, if you really need it, you will have to modify the MYSQL backend so that it can also handles that datatype.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi and welcome to devnet,

        AFAIK, no, if you really need it, you will have to modify the MYSQL backend so that it can also handles that datatype.

        F Offline
        F Offline
        funnydog
        wrote on last edited by
        #3

        @SGaist Thanks for the answer. What do you mean that "modify the MYSQL backend" ?

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

          You hvae to modify the Qt source code (qsql_mysql.cpp)

          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
          1
          • F funnydog

            @SGaist Thanks for the answer. What do you mean that "modify the MYSQL backend" ?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @funnydog
            All these complications! Without having to modify Qt/MySQL code, can't you just (untested) SELECT ..., CAST(json_column AS TEXT), ... in your statement (or, you could even put a VIEW in at the MySQL side if you don't want to have to do that each time, or don't know which columns are JSON at the client), and then handle it as JSON in your client code? It's a touch more code if you want to be able to write it back to the database, but you haven't asked for that.

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

              Strange that MYSQL is so stupid here. With PostgreSql it works out-of-the-box (it's returned as a QString)

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

              JonBJ 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                Strange that MYSQL is so stupid here. With PostgreSql it works out-of-the-box (it's returned as a QString)

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                Well that's down to the QMYSQL driver rather than MySQL itself, isn't it?

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

                  No, in PostgreSql it's returned as string type rather than a special json(b) type so it works out-of-the-box

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

                  JonBJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    No, in PostgreSql it's returned as string type rather than a special json(b) type so it works out-of-the-box

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher
                    Then it's PostgreSql which is "stupid" here, because it's not putting the special interpretation on JSON compared to plain text which MySQL does :)

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

                      Internally it's json and you can do queries on it, and that's the case for a very long time already: https://www.postgresql.org/docs/9.3/functions-json.html
                      If you don't pass valid json to such a column you also get an sql error.

                      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
                      0

                      • Login

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