PDA

View Full Version : QT-Database classes and UTF8 charsets in mysql



Wurgl
22nd August 2010, 20:49
I am wondering how to use UTF8 charset correct with mysql.

I have a database which looks like

mysql> show create database Testdb;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| testdb | CREATE DATABASE `Testdb` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+


Then I have a table which looks like this one:

mysql> show create table Source;
+--------+--------------
| Table | Create Table
+--------+----------------
| Source | CREATE TABLE `Source` (
`title` varchar(255) character set utf8 collate utf8_bin default NULL,
...
`id` mediumint(9) NOT NULL auto_increment,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
+--------+--------------


So wherever I can, there is information to use UTF-8 as the character set.

When inserting german umlaut chars, I see those in the database with the expected coding. However, when I retrieve records with those umlauts, I see garbage characters in my Qt-Application instead of those umlauts.

So far I did not find any way to tell the Qt-Classes which charset the data from the queries holds, however I expected that the classes retrieve that information.

As it seems, I have set various flags for the database itself also to uft8

mysql> show variables like "%char%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+


What did I miss? Any hints?

Thanks

yakin
22nd August 2010, 20:57
Is your application running in utf8 charset. (echo $LANG on X; on Windows, it should be UTF8)

You may try the following. I am not shure, if that helps?:confused:

QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("UTF-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecFor Name("UTF-8"));

Wurgl
22nd August 2010, 21:08
In general I have no problem with whatever chars, even chinese chars work ... although I do not understand what they mean :-)

But until now, I used my own class to retrieve databases, but I would like to use as much as I can from Qt.

For example this code:


QStandardItem *item = new QStandardItem();
item->setData("Ue: Ü", Qt::DisplayRole);
shows up as expected. Even in the same QTreeView.

The Storm
22nd August 2010, 21:24
When getting the data from the query use QString::fromUtf8() on the utf8 data only and you will get correct results. :)

P.S. I'm just guessing, I have never tried that myself.

Wurgl
22nd August 2010, 21:32
You mean something like (untested):

QSqlRecord record = query.record();
QString myValue = QString::fromUtf8(record.value(0).toString().toAsc ii());

looks really strange :-D

I thought that the database class finds the character set from the table definition, as it finds the names of columns, the types and all this stuff.