Perfectly solve the 1366 error of saving Chinese in MySQL

Most recently when using SQLAlchemy to store Chinese in a table in a mysql database:
Warning Code: 1366 penstring value: ‘\xE5\x9C\xA8’ for column ‘content’ at row 1
This is because we store Chinese, and our table does not support Chinese character set. We use show variables like ‘character%’. View mysql’s current encoding:

It can be seen that the character set of database and Server USES latin1, and latin1 does not support Chinese, which leads to the error of storing Chinese.
I tried two ways to avoid the Chinese storage error problem:
1: Set server and Database to UTF8 type
Use the following command to set input on the command line:
Show variables like ‘% char %’;
set character_set_server = utf8;
set character_set_database = utf8;
Use the command above. If not, show Create table. To see if the specific column has the wrong character set.
2: Set the default character set when building the table
When I build tables, I usually set the default character set to UTF8 in the SQL statement to avoid some problems:

sql='''create table analysis(city varchar(20),companySize varchar(20),education varchar(20))default charset=utf8'''

The default Charset = UTf8 is added at the end of the statement to set the default charset= UTf8, or at the end of the SQL statement if additional parameters need to be set.
 
★★★★★ ★★ warm tips:
This is a people encounter a problem, here said once: when using SQL statements, some of the words, letters is a class of special characters similar to python, import, etc., so if you want to use the key words, need to add the quotes ` to it, such as ` index `, the index is the key word, if used directly, will be an error. Therefore, I need to pay special attention to the fact that I always make mistakes when using it. I just can’t find the problem and think of other mistakes. Fortunately, I suddenly remember this problem mentioned by the database teacher accidentally.
 

Read More: