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.