Results 1 to 6 of 6

Thread: MySQL UTF-8 encoding issue

  1. #1
    Join Date
    Jul 2009
    Location
    Jordan, and UAE
    Posts
    55
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default MySQL UTF-8 encoding issue

    Hello Everyone

    I have another mysql UTF-8 problem, I have a table that has some records stored in it in UTF-8 format (some records are in English, and some of them are Arabic)

    my problem is that if I use php to retried the entries, they are encoded correctly and I can see both the Arabic and the English words. but I also need to use Qt to read the same entries and display them in a Qt table.

    The entry in the database looks like this in UTF-8 encoding (in php)
    دبي

    I'm doing it and I'm getting these values.
    ??دب

    the above line, has partially correct value, it gets 2 out of 3 characters correct, but I have NO idea why its NOT showing this correctly.

    Below is my code, and I would appreciate any suggestion you have.


    Qt Code:
    1. bool ret = query.exec("select name_searchword from searchword where id_company = 1125");
    2. while(query.next())
    3. {
    4. QByteArray ba = query.value(0).toByteArray();
    5. QString st = QString::fromUtf8( ba.data() );
    6. qDebug() << "Str is :" << st;
    7. }
    To copy to clipboard, switch view to plain text mode 

    The output is:

    Str is : دب??


    I've tried to use QTextCodec::setCodecForCStrings and set the value to UTF-8, but thats ONLY made it worse.

    I've also tried those 2 lines, together and one on its own, and still no results.

    Qt Code:
    1. QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8") );
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QTextCodec *codec = QTextCodec::codecForName("UTF8");
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot for your help

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: MySQL UTF-8 encoding issue

    why do you convert your result to an byte array? there you skip the informations. Just do
    Qt Code:
    1. bool ret = query.exec("select name_searchword from searchword where id_company = 1125");
    2. while(query.next())
    3. {
    4. QString st = query.value(0).toString();
    5. qDebug() << "Str is :" << st;
    6. }
    To copy to clipboard, switch view to plain text mode 
    and all should work fine.

  3. #3
    Join Date
    Jul 2009
    Location
    Jordan, and UAE
    Posts
    55
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MySQL UTF-8 encoding issue

    Hey Lykurg

    I tried this, but it gave me the following result
    Str is : "?¯?¨U‰"

    thankx, but what do you think is the reason?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: MySQL UTF-8 encoding issue

    Are you sure that your data in the database is encoded in utf8 correctly? Do you use any encoding function in php before printing it?

    You can try to set utf8 explicitly after establishing the connection to your database using:
    sql Code:
    1. SET NAMES 'utf8';
    2. SET CHARACTER SET 'utf8';
    To copy to clipboard, switch view to plain text mode 

    (Not sure right now if it has to be utf8 or utf-8...)

  5. #5
    Join Date
    Jul 2009
    Location
    Jordan, and UAE
    Posts
    55
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MySQL UTF-8 encoding issue

    I'm sure of the text is encoded correctly in UTF8, in PHP it shows if I use the following line at the beginning of the page
    <?php header("Content-type: text/html; charset=utf-8");

    and then I use code directly, and everything works out fine.

    This is how my Qt code looks like, and I still dont have the correct result, if you can see something that I'm missing, please point it out to me.
    Thanks

    Qt Code:
    1. QSqlQuery query(m_masterDatabase);
    2.  
    3. bool ret = query.exec("SET NAMES 'utf8'");
    4. if(ret)
    5. qDebug() << "true1";
    6. else
    7. qDebug() << query.lastError().text();
    8.  
    9. ret = query.exec("SET CHARACTER SET 'utf8'");
    10. if(ret)
    11. qDebug() << "true2";
    12. else
    13. qDebug() << query.lastError().text();
    14.  
    15. ret = query.exec("select name_searchword from searchword where id_company = 1125");
    16. while(query.next())
    17. {
    18. QString st = query.value(0).toString();
    19. qDebug() << "Str is :" << st;
    20. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: MySQL UTF-8 encoding issue

    Well right now it seems to me, that your database in not right encoded. Your Qt is fine.

    You could make a test: insert a new row to your database by using Qt. Make sure all is utf8. (use QString::fromUtf8() etc.) then querry that row and see if it is displayed correct.

Similar Threads

  1. Mysql and Arabic encoding issue
    By yazwas in forum Qt Programming
    Replies: 5
    Last Post: 23rd March 2011, 15:43
  2. Replies: 5
    Last Post: 25th September 2009, 14:03
  3. qt and mysql encoding problem
    By ferasodh in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2007, 09:48
  4. QT/mySQL deployment issue
    By smtgra011 in forum Installation and Deployment
    Replies: 4
    Last Post: 1st July 2007, 20:31
  5. Mysql LD_LIBRARY_PATH Issue
    By Think_Positive in forum Installation and Deployment
    Replies: 3
    Last Post: 13th February 2007, 19:16

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.