How to solve the problem of using MySQL in Qt 4

Source: Internet
Author: User

In MySQL 4.1 and later versions, the display of Qt Chinese characters is quite troublesome. Sometimes there may be question marks or garbled characters when using QT. Now I will explain how to use Qt correctly.

Create a table in MySQL and use Chinese characters. Generally, there are two types: GB (GB2312 or GBK) and UTF8. The two solutions are similar. Here we use GBK as an example.

1. Character-related fields in databases, tables, and tablesVarchar, char, text, etc.) all use the gbk_chinese_ci method. You can do this without doing so, but doing so saves a lot of trouble.

Ii. Re-compile the MySQL driver of QtModify the src/SQL/drivers/mysql/qsql_mysql.cpp file.

The part to be modified is as follows: the codec function in line 108th. Note that the red bold part is the added statement that I modified.

Code snippet

[C/C ++/Objective-C] Code

 
  1. Static QTextCodec * codec (MYSQL * mysql)
  2. {
  3. Return QTextCodec: codecForName ("GBK ");
  4. # If MYSQL_VERSION_ID >=32321
  5. QTextCodec * heuristicCodec = QTextCodec: codecForName (mysql_character_set_name (mysql ));
  6. If (heuristicCodec)
  7. Return heuristicCodec;
  8. # Endif
  9. Return QTextCodec: codecForLocale ();
  10. }
  11. // Then, re-compile the mysql driver of qt. I will not talk about it here. Please refer to the related documents of Qt.

3. Add the following two sentences at the beginning of the app in the main function of the Qt program.And the two are unnecessary troubles.

 
  1. QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));  
  2. QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));  
  3. //QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK")); 

4. After connecting to the Qt database, run the set names 'gbk' statement.

 
  1. db = QSqlDatabase::addDatabase("QMYSQL");  
  2. db.setHostName("localhost");  
  3. db.setDatabaseName("yourdatabase");  
  4. db.setUserName("yourusername");  
  5. db.setPassword("yourpassword");  
  6. db.open();  
  7. db.exec("SET NAMES 'GBK'");  

If you want to directly modify data through SQL statements, you need to correctly convert the input content into a GB code.

 
  1. QTextCodec * codec = QTextCodec: codecForName ("GBK ");
  2. QString strOut = codec-> fromUnicode (lineEdit-> text (). data ();
  3. QString strSql = "UPDATE user SET uinfo = '" + strOut + "'";
  4. Db.exe c (strSql );
  5. // If codecForTr is set to GBK, the preceding manual conversion step is not required.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.