The root of the problem
The main problem is that in the character set, the general solution to this problem is by experimentation. I experimented with a conclusion to share with you (in case of errors):
- The character set of the database
- The character set of the database connection
Configuration method
Set the database's character set to utf8mb4
: When you create the database ( Note : is the database), specify the character set (charset) and the contrast algorithm (collate), and then CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
collate what to do.
The character set for setting up the database connection is utf8mb4
: Specify the character set (charset) when establishing a MySQL database connection, and the following example connects the database posture with Pyton Pymsql:
Import Pymysqlparams= { ' host ': ' 127.0.0.1 ', ' Port ': 3306, < Span class= "hljs-string" > ' db ': ' Xueweihan ', ' user ': ' root ', ' password ': ' CharSet ': ' utf8mb4 '} # Set charsetconnection = pymysql. Connect (**params)
Different character sets, the contrast mode (collate) is not the same, so you need to specify the rules of comparison, utf8mb4_unicode_ci
is utf8mb4
the character set corresponding to the comparison rules. For example, if the character set is a utf8mb4
database, execute the SQL statement: select * from boy where name=‘xueweihan‘
you need to filter the records in the database with the specified collate rules.
Use Posture
To create a database:CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
To connect to a database:
Import Pymysqlparams= {' host ': ' 127.0.0.1 ', ' Port ': 3306, ' db ': ' Xueweihan ', ' user ': ' root ', ' Password ': ', ' charset ': ' utf8mb4 '} # set Charsetconnection = Pymysql. Connect (**params)
[MySQL] supports emoji (character set issues)!