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. QByteArray saves wrong size with UTF8
Forum Updated to NodeBB v4.3 + New Features

QByteArray saves wrong size with UTF8

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 382 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.
  • J Offline
    J Offline
    JAVH
    wrote on last edited by
    #1

    Hi, Im developing a tool who saves biometric data in MariaDB database, At the beggining of the program I do

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    

    And I can save and view text Ok (accents included), but when I try to save enrrollment to database (is a QByteArray) the database saves with a incorrect size, and cannot recognize fingerprint posteriorly, but, if I remove support forUTF8, I can save correctly the fingerprint in database, but text with accents shows incorrectly.

    I did try change collation in MariaDb without luck, and I dont know how to deactivate LocaleCodec temporary for saving ifingerprint n database.

    The way I create QByteArray and save data is this:

    Var.HuellaBin = QByteArray ((char*)(m_pEnrollmentFmd),m_nEnrollmentFmdSize);
    
    C 1 Reply Last reply
    0
    • J JAVH

      Hi, Im developing a tool who saves biometric data in MariaDB database, At the beggining of the program I do

      QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
      

      And I can save and view text Ok (accents included), but when I try to save enrrollment to database (is a QByteArray) the database saves with a incorrect size, and cannot recognize fingerprint posteriorly, but, if I remove support forUTF8, I can save correctly the fingerprint in database, but text with accents shows incorrectly.

      I did try change collation in MariaDb without luck, and I dont know how to deactivate LocaleCodec temporary for saving ifingerprint n database.

      The way I create QByteArray and save data is this:

      Var.HuellaBin = QByteArray ((char*)(m_pEnrollmentFmd),m_nEnrollmentFmdSize);
      
      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @JAVH
      What is the database column defined as?
      How are you inserting values into that column?
      What are you doing with the binary data that involves text encoding in any way?

      J 1 Reply Last reply
      1
      • C ChrisW67

        @JAVH
        What is the database column defined as?
        How are you inserting values into that column?
        What are you doing with the binary data that involves text encoding in any way?

        J Offline
        J Offline
        JAVH
        wrote on last edited by JAVH
        #3

        @ChrisW67 The column is longblob, charset is utf8mb4 and collation is utf8mb4_general_ci

        huella1.png

        I insert fingerprint in 2 ways, by a QSQlquery and calling a Stored procedure,

        if(Huellas==0)
                    {
                        Data.Query.clear();
                        Data.Query.prepare("insert t_huella (huellablob,id_empleado) values (?,?)");
                        Data.Query.bindValue(0,Var.HuellaBin);
                        Data.Query.bindValue(1,currentID);
                        Data.Query.exec();
                    }
                    else
                    {
                        if(Var.saveHuella) //se modificó la huella
                        {
                            Data.Query.clear();
                            Data.Query.prepare("update t_huella set huellablob=? where id_empleado=?");
                            Data.Query.bindValue(0,Var.HuellaBin);
                            Data.Query.bindValue(1,currentID);
                            Data.Query.exec();
                            Var.saveHuella=false;  //se restaura indicador de guardar huella una vez que ya se guardó
                        }
                    }
        

        This works fine when I disable locale UTF8 at the beggining of the aplication, but its show wrong characters with accents, if I put this line at beggining:

        QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
        

        Text show correctly, but if I want save a fingerprint, the long of QBytearray to save is more than double of large and fingerprint SDK cannot recognize data saved:

        Values in Database:

        huella2.png

        Christian EhrlicherC J 2 Replies Last reply
        0
        • J JAVH

          @ChrisW67 The column is longblob, charset is utf8mb4 and collation is utf8mb4_general_ci

          huella1.png

          I insert fingerprint in 2 ways, by a QSQlquery and calling a Stored procedure,

          if(Huellas==0)
                      {
                          Data.Query.clear();
                          Data.Query.prepare("insert t_huella (huellablob,id_empleado) values (?,?)");
                          Data.Query.bindValue(0,Var.HuellaBin);
                          Data.Query.bindValue(1,currentID);
                          Data.Query.exec();
                      }
                      else
                      {
                          if(Var.saveHuella) //se modificó la huella
                          {
                              Data.Query.clear();
                              Data.Query.prepare("update t_huella set huellablob=? where id_empleado=?");
                              Data.Query.bindValue(0,Var.HuellaBin);
                              Data.Query.bindValue(1,currentID);
                              Data.Query.exec();
                              Var.saveHuella=false;  //se restaura indicador de guardar huella una vez que ya se guardó
                          }
                      }
          

          This works fine when I disable locale UTF8 at the beggining of the aplication, but its show wrong characters with accents, if I put this line at beggining:

          QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
          

          Text show correctly, but if I want save a fingerprint, the long of QBytearray to save is more than double of large and fingerprint SDK cannot recognize data saved:

          Values in Database:

          huella2.png

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why store a QString in a blob?
          Please provide a minimal, compilable example. What is Var.HuellaBin ? How is it created?

          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
          • J JAVH

            @ChrisW67 The column is longblob, charset is utf8mb4 and collation is utf8mb4_general_ci

            huella1.png

            I insert fingerprint in 2 ways, by a QSQlquery and calling a Stored procedure,

            if(Huellas==0)
                        {
                            Data.Query.clear();
                            Data.Query.prepare("insert t_huella (huellablob,id_empleado) values (?,?)");
                            Data.Query.bindValue(0,Var.HuellaBin);
                            Data.Query.bindValue(1,currentID);
                            Data.Query.exec();
                        }
                        else
                        {
                            if(Var.saveHuella) //se modificó la huella
                            {
                                Data.Query.clear();
                                Data.Query.prepare("update t_huella set huellablob=? where id_empleado=?");
                                Data.Query.bindValue(0,Var.HuellaBin);
                                Data.Query.bindValue(1,currentID);
                                Data.Query.exec();
                                Var.saveHuella=false;  //se restaura indicador de guardar huella una vez que ya se guardó
                            }
                        }
            

            This works fine when I disable locale UTF8 at the beggining of the aplication, but its show wrong characters with accents, if I put this line at beggining:

            QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
            

            Text show correctly, but if I want save a fingerprint, the long of QBytearray to save is more than double of large and fingerprint SDK cannot recognize data saved:

            Values in Database:

            huella2.png

            J Offline
            J Offline
            JAVH
            wrote on last edited by
            #5

            Ok I did solve, I encoded the QByteArray to base64 and I saved to Database, when I get it, I decode data and after compare fingerprint

            Saving/Updating to DB

            Data.Query.bindValue(0,Var.HuellaBin.toBase64());
            

            Loading from DB:

            Huella=QByteArray::fromBase64(Data.Query.value(2).toByteArray());
            

            Thanks!

            C 1 Reply Last reply
            0
            • J JAVH has marked this topic as solved on
            • J JAVH

              Ok I did solve, I encoded the QByteArray to base64 and I saved to Database, when I get it, I decode data and after compare fingerprint

              Saving/Updating to DB

              Data.Query.bindValue(0,Var.HuellaBin.toBase64());
              

              Loading from DB:

              Huella=QByteArray::fromBase64(Data.Query.value(2).toByteArray());
              

              Thanks!

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

              @JAVH The longblob column should have the pseudo-character set "binary" (see, e.g., the second paragraph here) The blob is not a string with UTF-8 or any other encoding.

              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