How to use MySQL Chinese garbled characters in QT in Linux and Windows

Source: Internet
Author: User
This method should be the quickest way to solve Chinese Garbled text. You do not need to change the mysql encoding. It is only used in linux: 1. QTextCode in the main. cpp function of the QT Program

This method should be the quickest way to solve Chinese Garbled text. You do not need to change the mysql encoding. It is only used in linux: 1. QTextCode in the main. cpp function of the QT Program

Linux

This method should be the quickest way to solve Chinese Garbled text. You do not need to change the mysql encoding, but only use it in linux:

1. In the main function of main. cpp of the QT program, the three sentences QTextCodec "are as follows:

2. After setting the database in your program code, add db.exe c ("set names 'latin1'"); // use the database's Latin1 encoding directly.

The above two steps solve the problem.

Windows

After this method is modified, the Chinese can be displayed in QT normally, or the Chinese can be inserted into MYSQL through QT and displayed on the QT control. But when you view the table content through the command line, the Chinese text is displayed ?. Therefore, after this method is done, you cannot view mysql Chinese content in the command line.

The first step is to change the database encoding !!

How to solve the problem by using Chinese Characters in MySQL and later versions in Qt4

In MySQL and later versions, Chinese characters are troublesome. Sometimes, question marks or garbled characters may occur when used in QT. Now, I want to explain how to use them correctly in Qt.

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. The gbk_chinese_ci method must be used for character-related fields (such as varchar, char, and text) in databases, tables, and tables. You can do this without doing so, it saves a lot of trouble (for the setup method, refer to MySQL unified encoding).


2. To re-compile the MySQL driver of Qt, modify 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.

Quote:

Static QTextCodec * codec (MYSQL * mysql)
{
Return QTextCodec: codecForName ("GBK ");
# If MYSQL_VERSION_ID >=32321
QTextCodec * heuristicCodec = QTextCodec: codecForName (mysql_character_set_name (mysql ));
If (heuristicCodec)
Return heuristicCodec;
# Endif
Return QTextCodec: codecForLocale ();
}

Then, re-compile the mysql driver of qt. I will not talk about it here. Please refer to the Qt documentation.
3. Add the following two sentences at the beginning of the app in the main function of the Qt program, which saves unnecessary trouble.

Quote:

QTextCodec: setCodecForLocale (QTextCodec: codecForName ("GBK "));
QTextCodec: setCodecForCStrings (QTextCodec: codecForName ("GBK "));
// QTextCodec: setCodecForTr (QTextCodec: codecForName ("GBK "));


4. After connecting to the Qt database, run the set names 'gbk' statement (in red ).

Quote:

Db = QSqlDatabase: addDatabase ("QMYSQL ");
Db. setHostName ("localhost ");
Db. setDatabaseName ("yourdatabase ");
Db. setUserName ("yourusername ");
Db. setPassword ("yourpassword ");
Db. open ();
Db.exe c ("set names 'gbk '");


OK. After completing the above four steps, we can correctly view the Chinese data in MySQL in the program.
However, if you want to directly modify data through SQL statements, you need to correctly convert the input content into a GB code ,,
For example:

Quote:

QTextCodec * codec = QTextCodec: codecForName ("GBK ");
QString strOut = codec-> fromUnicode (lineEdit-> text (). data ();
QString strSql = "UPDATE user SET uinfo = '" + strOut + "'";
Db.exe c (strSql );


Note that if codecForTr is set to GBK, the preceding manual conversion step is not needed. That is, if the comment of the third row is removed from step 3, the preceding string format conversion is not required.

The most important point here is to modify the qsql_mysql.cpp file because it cannot correctly set the codec internal code.

If the encoding in the database uses UTF8, change the related section above to UTF-8 (Qt) or UTF8 (MySQL.

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.