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