A/C + + operation MySQL Character set

Source: Internet
Author: User
Tags mysql client

Linux, when we use the MySQL C API to operate the MYSQLDB, by default, the program will be Latin1 this character set to do SQL operations, this situation, it is likely to appear garbled,

For example, the character set of the database is UTF8, and then we insert a record into it through the program, and then we go to the query, there will be garbled.

To solve this problem, we can call int mysql_set_character_set (MySQL *mysql, const char *csname) This function, go to each connection to MySQL set corresponding character set, can solve garbled problem. For example, the following code:

MYSQL mysql;mysql_init(&mysql);if(!mysql_real_connect(&mysql,"host","user","passwd","database",0,NULL,0)){    fprintf(stderr, "Failed to connect to database: Error: %s\n",          mysql_error(&mysql));}if(!mysql_set_character_set(&mysql, "utf8")){    printf("New client character set: %s\n",           mysql_character_set_name(&mysql));}

Sometimes, when we go directly to the MySQL client to do SQL operations, in order to solve the garbled problem, generally before executing an SQL statement, will be executed: set names * * *,

This functionmysql_set_character_set() 跟set names *** 这条语句的作用是一样的。

A/C + + operation MySQL Character set

Related Article

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.