Results 1 to 5 of 5

Thread: QT-Database classes and UTF8 charsets in mysql

  1. #1
    Join Date
    Apr 2006
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QT-Database classes and UTF8 charsets in mysql

    I am wondering how to use UTF8 charset correct with mysql.

    I have a database which looks like
    Qt Code:
    1. mysql> show create database Testdb;
    2. +----------+-----------------------------------------------------------------+
    3. | Database | Create Database |
    4. +----------+-----------------------------------------------------------------+
    5. | testdb | CREATE DATABASE `Testdb` /*!40100 DEFAULT CHARACTER SET utf8 */ |
    6. +----------+-----------------------------------------------------------------+
    To copy to clipboard, switch view to plain text mode 

    Then I have a table which looks like this one:
    Qt Code:
    1. mysql> show create table Source;
    2. +--------+--------------
    3. | Table | Create Table
    4. +--------+----------------
    5. | Source | CREATE TABLE `Source` (
    6. `title` varchar(255) character set utf8 collate utf8_bin default NULL,
    7. ...
    8. `id` mediumint(9) NOT NULL auto_increment,
    9. PRIMARY KEY (`id`)
    10. ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
    11. +--------+--------------
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. mysql> show variables like "%char%";
    2. +--------------------------+----------------------------+
    3. | Variable_name | Value |
    4. +--------------------------+----------------------------+
    5. | character_set_client | utf8 |
    6. | character_set_connection | utf8 |
    7. | character_set_database | utf8 |
    8. | character_set_filesystem | binary |
    9. | character_set_results | utf8 |
    10. | character_set_server | utf8 |
    11. | character_set_system | utf8 |
    12. | character_sets_dir | /usr/share/mysql/charsets/ |
    13. +--------------------------+----------------------------+
    To copy to clipboard, switch view to plain text mode 

    What did I miss? Any hints?

    Thanks

  2. #2
    Join Date
    Dec 2007
    Posts
    27
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT-Database classes and UTF8 charsets in mysql

    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?
    Qt Code:
    1. QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    2. QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2006
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT-Database classes and UTF8 charsets in mysql

    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:
    Qt Code:
    1. item->setData("Ue: Ü", Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 
    shows up as expected. Even in the same QTreeView.

  4. #4
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT-Database classes and UTF8 charsets in mysql

    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.

  5. #5
    Join Date
    Apr 2006
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT-Database classes and UTF8 charsets in mysql

    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.

Similar Threads

  1. problem creating a mysql database
    By TonyB in forum Qt Programming
    Replies: 10
    Last Post: 23rd July 2010, 15:39
  2. MySQL database ,Garbled
    By weixj2003ld in forum Qt Programming
    Replies: 7
    Last Post: 9th June 2009, 14:24
  3. Putting and getting images from MySQL database?
    By allensr in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2007, 21:47
  4. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48
  5. Replies: 8
    Last Post: 7th March 2006, 13:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.