Call the api in C/C ++ to set the encoding method of the mysql connection.

Source: Internet
Author: User

Mysql uses the following methods to call APIs in C/C ++ to set the encoding method for connecting to mysql:

1. mysqli_set_charset
Call example:

[Cpp]
Ret = mysql_set_character_set (mysql, "utf8 ");

Ret = mysql_set_character_set (mysql, "utf8"); Description:
We recommend that you use the setting method. After you disconnect from mysql and automatically reconnect to mysql, you can still maintain the set encoding format, and affect the function of mysql_real_escape_string, so that the mysql_real_escape_string function uses the set encoding format to escape strings.
However, this function is supported only after mysql5.0.5, so the version is too low ....
2. Execute the SQL statement: SET NAMES
Call example:
[Cpp]
Ret = mysql_real_query (mysql, "set names UTF8 ;",
(Unsigned long) strlen ("set names UTF8 ;"));

Ret = mysql_real_query (mysql, "set names UTF8 ;",
(Unsigned long) strlen ("set names UTF8;"); Description:
SQL statement execution can only affect the current connection to the database. After automatic reconnection is disabled, the encoding format is reset to the default configuration.
3. Set the MYSQL_SET_CHARSET_NAME attribute
Call example:
[Cpp]
Ret = mysql_options (mysql, MYSQL_SET_CHARSET_NAME, "utf8 ");

Ret = mysql_options (mysql, MYSQL_SET_CHARSET_NAME, "utf8"); Description:
Similar to mysql_set_character_set, the configured encoding format is retained after automatic reconnection is disabled, but does not affect the mysql_real_escape_string function.
This method can be used in MySQL 5.0.5 and later versions.
Note that this attribute takes effect only after mysql_real_connect is called to connect to the database.

 

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.