When the MySQL common command set younger brother recently used MySQL c api for a project, he encountered a MySQL database character set problem. I checked some information and posted the MySQL command to share with you (MySQL version ):
1. view the character set used by the database, and type the command show variables like 'character/_ set/_ % ';
Mysql> show variables like 'character/_ set/_ % ';
+ -------------------------- + -------- +
| Variable_name | Value |
+ -------------------------- + -------- +
| Character_set_client | latin1 |
| Character_set_connection | latin1 |
| Character_set_database | latin1 |
| Character_set_results | latin1 |
| Character_set_server | latin1 |
| Character_set_system | utf8 |
+ -------------------------- + -------- +
6 rows in set (0.13 sec). the default character set installed is latin1.
2. modify the database character set. two methods are provided here.
First, modify the configuration file my. ini in the installation directory and change default-character-set = latin1 to the character set required by default-character-set =. Valid for restart.
Second, set character set utf8. the current session is valid and the restart fails. Therefore, the first method is usually used.
3. when using MySQL c api in Windows, in addition to adding the MySQL header file and library file to the project, you also need to modify lines 63-65 in mysql. h.
# Ifndef my_socket_defined
# Ifdef _ WIN __
# Define my_socket SOCKET
Is
# Ifndef my_socket_defined
# Ifndef _ WIN __
# Define my_socket SOCKET
Otherwise, an error occurs during compilation.
4. in the program, when using the mysql_query method of MySQL c api, if the query string contains Chinese characters, you need to first execute mysql_query (pMySQL, "set character set utf8"); where, the first parameter is an object of the MYSQL * type, and the second parameter is an actually executed statement, which means that the character set is set to utf8 character set that supports Chinese encoding.
5. commands for importing and exporting MySQL database data
Export data:
Mysqldump-u userName-p-A> c:/temp. SQL -- Export all data in the database to the temp. SQL file.
Mysqldump-u userName-p DatabaseName> c:/temp. SQL -- only exports the content of a database to the temp. SQL file.
Mysqldump-u userName-p DatabaseName TableName> c:/temp. SQL -- export a specific table in the database to the temp. SQL file.
Import data:
In the command line mysql> enter source C:/temp. SQL to import the exported data to the data.
6. character set commands for viewing a database or a data table
Show create database dbName
Show create table tbName