Results 1 to 2 of 2

Thread: QSqlQuery text encoding change MySQL4 -> MySQL5

  1. #1
    Join Date
    Jan 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlQuery text encoding change MySQL4 -> MySQL5

    Hi,

    We've just updated our software platform from CentOS 4 to CentOS 5, which entails an upgrade of MySQL from 4.1 to 5.0. After this upgrade I am unable to convert text retrieved from the database back into utf-8. The problem occurs in both Qt 4.3.5 and 4.6.0.

    Details:
    * The database column is varchar(100) and database is MyISAM with default charset = latin1.
    * The text data is encoded in utf-8 before it's ingested into the database with the MySQL CLI.
    * Querying the text in the MySQL CLI displays the correct text (arabic, hindi, etc).

    On CentOS 4, with MySQL 4.1 and Qt4.x, the following code retrieves the text correctly:
    Qt Code:
    1. QTextCodec* codec = QTextCodec::codecForName("utf-8");
    2. QString name = codec->toUnicode(query.value(1).toByteArray());
    To copy to clipboard, switch view to plain text mode 
    The same code on CentOS 5, with MySQL 5 and Qt 4.x returns garbled text for all non-latin1 characters. Please help.

    Best regards,
    John

  2. #2
    Join Date
    Jan 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery text encoding change MySQL4 -> MySQL5

    Hi guys,

    The problem is not solved, but I've made a test db, a test program and a perl script for testing which gives some interesting results. All test programs and test results are available in the attached zip file.

    1) When querying the database with the perl script, I get the correct results both on CentOS4 with MySQL 4.1 and CentOS5 with MySQL 5:
    Qt Code:
    1. % ./testquery.pl -db testquery -query "select * from station_info"
    2. Host: localhost
    3. Database: testquery
    4. Query: select * from station_info
    5.  
    6. Answer: 80821 2833 35.43 -5.54 -9999 10 60101 MO طنجة
    7. Answer: 80822 2833 35.88 -5.31 -9999 10 60320 MO سبتة
    8. Answer: 80823 2379 17.09 -20.83 -9999 10 99999 MO الكويرة
    9. Answer: 1608 3108 49.0333 13.2333 612 1 10796 Germany Plzeň
    10. Answer: 1808 3198 50.6833 14.0333 377 1 11502 Czech Republic ÚstÃ* n. L.
    11. Answer: 1814 3108 48.9333 14.45 432 1 11541 Czech Republic Č. Budějovice
    To copy to clipboard, switch view to plain text mode 
    Hence, I don't think there is anything wrong with the database.

    2) When running the qt program I get different results in the last column. When running the test program on CentOS4 w/ MySQL 4.1, I get:
    But when running the program on CentOS5 w/MySQL 5, I get

    Both platforms are using Qt 4.3.5. I get the same faulty last colum on CentOS5 w/MySQL 5 when using Qt 4.6.

    Here's the code of the the test program:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3.  
    4. bool createConnection()
    5. {
    6. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    7. db.setHostName("localhost");
    8. db.setDatabaseName("testquery");
    9. db.setUserName("guest");
    10. db.setPassword("guest");
    11.  
    12. if (!db.open()) {
    13. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    14. qApp->tr("Unable to establish a database connection.\n"
    15. "This example needs MySQL support. Please read "
    16. "the Qt SQL driver documentation for information how "
    17. "to build it.\n\n"
    18. "Click Cancel to exit."), QMessageBox::Cancel);
    19. return false;
    20. }
    21. return true;
    22. }
    23.  
    24. void populateTableFromDatabase(QTableWidget* table)
    25. {
    26. QTextCodec* codec = QTextCodec::codecForName("utf-8");
    27. QString cmd("SELECT id, country, placename FROM station_info");
    28. int i = 0;
    29.  
    30. QSqlQuery query(cmd);
    31. if( query.isActive() )
    32. {
    33. table->setRowCount(query.size());
    34. table->setColumnCount(4);
    35. QStringList labels;
    36. labels << "ID" << "Country" << "From Latin1" << "From UTF-8";
    37. table->setHorizontalHeaderLabels(labels);
    38.  
    39. while ( query.next() )
    40. {
    41. double id = query.value(0).toInt();
    42. QString country = query.value(1).toString();
    43. QString placename1 = query.value(2).toString();
    44. QString placename2 = codec->toUnicode(query.value(2).toByteArray());
    45.  
    46. table->setItem(i,0,new QTableWidgetItem(QString::number(id)));
    47. table->setItem(i,1,new QTableWidgetItem(country));
    48. table->setItem(i,2,new QTableWidgetItem(placename1));
    49. table->setItem(i,3,new QTableWidgetItem(placename2));
    50.  
    51. i++;
    52. }
    53. }
    54. }
    55.  
    56.  
    57. int main(int argc, char *argv[])
    58. {
    59. QApplication app(argc, argv);
    60. if (!createConnection())
    61. return 1;
    62.  
    63. QHBoxLayout *hbox = new QHBoxLayout();
    64. QTableWidget *table = new QTableWidget();
    65. populateTableFromDatabase(table);
    66. hbox->addWidget(table);
    67.  
    68. QWidget widget;
    69. widget.setLayout(hbox);
    70. widget.setWindowTitle("testquery: MySQL 5");
    71. widget.resize(450,240);
    72. widget.show();
    73.  
    74. return app.exec();
    75. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance for any help or tips

    Best regards,
    John
    Attached Images Attached Images
    Attached Files Attached Files

Similar Threads

  1. How to change the Label text
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 15th September 2009, 12:57
  2. Change value inside QTableView or QSqlQuery
    By jano_alex_es in forum Newbie
    Replies: 1
    Last Post: 15th May 2009, 10:49
  3. Character encoding in text edit and llne edit
    By greenvirag in forum Qt Programming
    Replies: 3
    Last Post: 20th January 2009, 08:45
  4. Problems with text encoding
    By LMZ in forum Qt Programming
    Replies: 4
    Last Post: 3rd July 2007, 12:49
  5. Replies: 8
    Last Post: 15th May 2007, 09:21

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