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. SQLite BLOB to QString
Qt 6.11 is out! See what's new in the release blog

SQLite BLOB to QString

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 3.4k Views 2 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.
  • CryptixDEC Offline
    CryptixDEC Offline
    CryptixDE
    wrote on last edited by CryptixDE
    #1

    I have a SQLite database with a field typed BLOB (Binary),
    but this field contains normal text.
    I want to have this field value as QString.
    I already was able to get the value as ByteArray.

    Here is my current code:

    QSqlQuery query("SELECT value FROM Tabelle WHERE key='token'", db);
    query.first();
    QString data= query.value(0).toString();
    

    But what I get is: "\""

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

      Hi
      Did you try to convert to string via ByteArray ?
      Here you do directly from QVariant

      1 Reply Last reply
      0
      • CryptixDEC Offline
        CryptixDEC Offline
        CryptixDE
        wrote on last edited by CryptixDE
        #3

        How do I do this?
        I searched for it but I couldn't find a way to do this via QVariant

        Edit: Yes, I did, the SQLite result is aQVariant

        mrjjM 1 Reply Last reply
        0
        • CryptixDEC CryptixDE

          How do I do this?
          I searched for it but I couldn't find a way to do this via QVariant

          Edit: Yes, I did, the SQLite result is aQVariant

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @CryptixDE
          Hi
          did not check if even possible but i meant like
          QByteArray data= query.value(0).toByteArray();
          QString datastr(data);

          CryptixDEC 1 Reply Last reply
          0
          • F Offline
            F Offline
            fsilva
            wrote on last edited by
            #5

            This will depend a lot on the actual data that is present in the BLOB column ("text" can mean many many different things at the binary level).

            But for a more generic case, something like this should work:

            QByteArray strData = query.value(0).toByteArray();
            QString str = QString::fromUtf8(strData);
            
            1 Reply Last reply
            1
            • mrjjM mrjj

              @CryptixDE
              Hi
              did not check if even possible but i meant like
              QByteArray data= query.value(0).toByteArray();
              QString datastr(data);

              CryptixDEC Offline
              CryptixDEC Offline
              CryptixDE
              wrote on last edited by CryptixDE
              #6

              @mrjj Yea, I tried this, but this not returns the string for me, it just returns "\""

              @fsilva Thank you, I'll try it asap :)

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fsilva
                wrote on last edited by
                #7

                You need to know the encoding of your string (I assumed UTF-8 but if you are getting only the quotation marks it's most likely UTF-16) and use the appropriate QString constructor or from**** static method.

                CryptixDEC 1 Reply Last reply
                2
                • F fsilva

                  You need to know the encoding of your string (I assumed UTF-8 but if you are getting only the quotation marks it's most likely UTF-16) and use the appropriate QString constructor or from**** static method.

                  CryptixDEC Offline
                  CryptixDEC Offline
                  CryptixDE
                  wrote on last edited by
                  #8

                  @fsilva so fromUtf16 instead of fromUtf8 should work?

                  1 Reply Last reply
                  0
                  • CryptixDEC Offline
                    CryptixDEC Offline
                    CryptixDE
                    wrote on last edited by
                    #9

                    Yea, I solved this issue by using the hex value

                    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