Solved How can i read something that is encoded in UTF-8?
-
I have a MySQL Database that storage some Castilian words that have accents and "Ñ". All those words are encoded in utf-8 and I tried to read it using the following lines:
while(query.next()){ qDebug() << query.value(query.record().indexOf("GPROF")); }
But that algorithm fails when tries to read those types of character.
-
Hi,
Is GPROF a name to search ? Or is it just an example ?
-
@SGaist Its an example of my DB
-
Does indexOf() fail when name contains accents ?
-
@mrjj Yes, it cannot read accents
-
Can you provide an example of such a table and how do create it ?
By the way, which version of Qt, OS, MySQL are you using ?
-
@SGaist What ever i write in MySQL Workbench 6.3 that contains "Ñ" or " ´ " (accent) cannot be read by Qt 5.8 LGPL . My OS is Windows 7.
I use this:
QSqlQuery query(QSqlDatabase::database("Conexion1")); query.prepare("SELECT * FROM año20171"); query.exec(); while(query.next()){ int b = query.record().indexOf("NOMBRE") ; QString a = query.value(b).toString(); // With this method i can get CARLOS but not FÉRNANDO instead of it i get FÉRNANDO }
ID_PROF | NOMBRE | GPROF | GRADO |........................................ 1 | CARLOS | 100 | TERCERO |.......................................... 2 | FÉRNANDO | 200 | SEGUNDO |.........................................................
If i write QString::fromutf8("FÉRNANDO") qDebug() shows "FÉRNANDO"
-
Ive finnally found the solution you only have to add the following line after QString a:
a = a.toLocal8Bit();
It will transform all those illegible signatures in characters with tilde
-
The output of qDebug will partly depend on your locale as well as it will escape non-printable characters.