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. [SOLVED] How to convert char from SQL into QChar?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to convert char from SQL into QChar?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 3.0k 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.
  • M Offline
    M Offline
    MisteKiste
    wrote on last edited by
    #1

    Hello,

    I got a SQL Database with char field. Now i want to fetch the char field and convert it into QChar. But it doesn't really work. I tried to convert with
    @
    .toChar
    @

    Shouldn't it work? It returned always
    @0 '\0'@

    How to convert char from SQL correctly into QChar?

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      Can you show your code that fetches the char from the SQL database?

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MisteKiste
        wrote on last edited by
        #3

        Yes.

        @

        QSqlQuery query;
        query.prepare(queryStatement);
        int field_Cgender = query.record().indexOf("Cgender");

        while ( query.next() )
        {
        PointsWeightsMD * obj_Result = new PointsWeightsMD();

            obj_Result->c_Gender = query.value(field_Cgender).toChar();
        
            objs_Result.append(obj_Result);
        }
        

        @

        But I think thats not a Database problem. I can fetch the other fields without problems.

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          Is the value of field_Cgender correct?

          What is the raw value and type retrieved from the query?

          @
          #include <QDebug>

          ...

          while ( query.next() )
          {
          qDebug() << field_Cgender << query.value(field_Cgender);
          }
          @

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MisteKiste
            wrote on last edited by
            #5

            This is the output:

            bq. 3 QVariant(QString, "m")bq.

            Thats weird. Why its a QString? In Database its stored as char...

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              What type of SQL implementation is it? (SQLite? SQL Server? PostgreSQL? Something else?) In all the implementations I've seen, CHAR is designed for storing character arrays (which means they store strings). For example, see http://dev.mysql.com/doc/refman/5.0/en/char.html

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MisteKiste
                wrote on last edited by
                #7

                I'm using SQLite. So when I'm fetching data with SQL Statement I have to store it in a QString variable and after that I can convert into QChar?
                Well that is exactly what I'm doing here or not?
                @obj_Result->c_Gender = query.value(field_Cgender).toChar();@

                Edit:
                Ok...weird...I can't even edit the value of c_Gender in Debug modus. Its always 0. Can somebody explain why?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MisteKiste
                  wrote on last edited by
                  #8

                  Has nobody an idea? :(

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    clochydd
                    wrote on last edited by
                    #9

                    Hi,
                    JKSH already stated, that the SQL Database stores CHAR as strings.
                    Working with Postgres I read all CHAR, VARCHAR, TEXT into QString and never had a problem with that.
                    In your case:
                    @
                    obj_Result->c_Gender = query.value(field_Cgender).toString();
                    @
                    You should then be able to convert the QString into QChar.
                    Hope it helps...

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      SQLite doesn't even distinguish between column types. Not really.

                      If you want the char from your string, just use

                      @
                      QString str_Result->c_Gender = query.value(field_Cgender).toString();
                      QChar obj_result;
                      if (!str_Result.isEmpty()) {
                      obj_result = str_Result[0];
                      }
                      @

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        MisteKiste
                        wrote on last edited by
                        #11

                        Thanks guys that helped me a lot. I thought it just works with .toChar...

                        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