C ++ writes or reads Chinese characters to or from MySQL

Source: Internet
Author: User

 

At work, we need to insert and read and write Chinese data to MySQL. This problem was tricky at first. After a period of investigation, the problem was solved smoothly.

The solution is as follows:

 

1. Set the character set of the MySQL server. The default Character Set of the MySQL server is utf8;

 

2. Set the character set of a table or column. It is best to use UTF-8 and support both Chinese and English.

 

3. After the connection, call the int mysql_set_character_set (MySQL * MySQL, char * csname) interface. This function sets the default character set for the current connection.

Note that only MySQL 5.0.7 and later versions support this function. In earlier versions, the set names <character setname> statement can be used instead.

 

4. When reading and writing data to MySQL, convert all strings to utf8 encoding. The English utf8 encoding is the same as that of ANSI, and there are differences between Chinese characters.

 

The above is my solution, if there is a problem, please contact me <bicheng.gui@gmail.com>.

 

TheArticleOriginal. Please note the post!

 


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.