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 Update on Monday, May 27th 2025

Is QSqlQuery not support json type in mysql ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.6k 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.
  • F Offline
    F Offline
    funnydog
    wrote on 23 Mar 2019, 16:40 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Mar 2019, 21:54 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 24 Mar 2019, 07:17
      2
      • S SGaist
        23 Mar 2019, 21:54

        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 24 Mar 2019, 07:17 last edited by
        #3

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

        J 1 Reply Last reply 24 Mar 2019, 08:46
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 24 Mar 2019, 07:29 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
            24 Mar 2019, 07:17

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

            J Offline
            J Offline
            JonB
            wrote on 24 Mar 2019, 08:46 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
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 24 Mar 2019, 10:33 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

              J 1 Reply Last reply 24 Mar 2019, 11:04
              0
              • C Christian Ehrlicher
                24 Mar 2019, 10:33

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

                J Offline
                J Offline
                JonB
                wrote on 24 Mar 2019, 11:04 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
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 24 Mar 2019, 11:22 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

                  J 1 Reply Last reply 24 Mar 2019, 11:27
                  0
                  • C Christian Ehrlicher
                    24 Mar 2019, 11:22

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 24 Mar 2019, 11:27 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
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 24 Mar 2019, 12:43 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

                      1/10

                      23 Mar 2019, 16:40

                      • Login

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