QByteArray saves wrong size with UTF8
-
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);
-
@ChrisW67 The column is longblob, charset is utf8mb4 and collation is utf8mb4_general_ci
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:
-
Why store a QString in a blob?
Please provide a minimal, compilable example. What is Var.HuellaBin ? How is it created? -
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!
-