PDA

View Full Version : SQLite connection and encoding



mhbeyle
2nd December 2009, 15:48
Hello.

I'm having some encoding problems when QT SQLite connection ( QSqlDatabase::addDatabase("QSQLITE") ) tries to read data from a SQLite archive.
I assign a field value from the database into a QString variable:


QString str001;
str001 = record.value("title").toString();

I assume that the field contains a UTF-16 encoding (SQLite3 documentation) and I assume that QT4 processes information as UTF-16 unless I use specific functions. And I'm triyng to compile my program under Windows XP SP3 that uses UCS-2 / UTF-16 for heir standard Unicode encoding.
But when field information displays on the screen I obtain the "?" code for non-standard chars like accentuated vowels or another specific symbols.
I have tried to display the information in a label, a text box, a list box, a table, ... But always is the same. If QT display standard QStrings (not from SQLite), the output is encoding correctly.
Is there any way of finding out what is getting lost in the conversion process? Something that warns about the incorrect codification (in the database, the driver, in my head, ...) or something like that.


Thanks.

wysota
3rd December 2009, 10:02
Hmm... as far as I know SQLite uses UTF-8 by default.

mhbeyle
3rd December 2009, 15:25
Hmm... as far as I know SQLite uses UTF-8 by default.

Finally I resolved the issue... ;)

SQLite3 adds support for both UTF-8 and UTF-16 text and SQLite driver in QT4 uses UTF-16 functions when opens and manipulates the database (qsql_sqlite.cpp -> sqlite3_open16).
The problem was that I was creating the database with SQLite Administrator (http://sqliteadmin.orbmu2k.de/) and this tool seemingly doesn't support UTF-16 or, at least, it creates the database in UTF-8 by default. I noticed about this behavior when I opened the database with a hex-editor. Chars were written with UTF-8 encoding and one byte.
And the problem grows more when QT believes that data is UTF-16 encoding because SQLite driver processes data in this manner. The conversion functions do not work as it stems and QT4 writes and reads the data with two bytes but in UTF-8. A real foul-up :eek:

Of course and finally, I created the db in UTF-16 :rolleyes:

Thank you very much for your attention.