This article describes the python in the mysqldb garbled solution, share to everyone for your reference. Here's how:
In general, the most troublesome problem in using MySQL is garbled.
To view the code for MySQL:
Command: Copy the Code code as follows:
Show variables like ' character_set_% ';
You can see the following results:
Character_set_client is the client-side encoding method;
Character_set_connection the encoding used to establish the connection;
Coding of character_set_database database;
Character_set_results the encoding of the result set;
Character_set_server the encoding of the database server;
as long as the above four is guaranteed to use the same encoding method, there will be no garbled problem .
You can then set the MySQL encoding directly here.
Copy the Code code as follows:
Set character_set_client = xxxxx
This modifies the client code.
However, for databases and data tables that have been established, the encoding remains unchanged, or the alter command is used to change the corresponding encoding .
However, even if you modify the encoding of the database, in Python there will be stored in the database when the garbled problem, the solution is to link the database when the code is specified . For example:
Copy the Code code as follows:
Sql_con = MySQLdb.connect (host=mysql_addr, User=mysql_user, Passwd=mysql_pwd, db=mysql_db, charset= "UTF8")
This specifies that the encoding of the client is UTF8. Then it solves the problem of garbled characters.
Hopefully this article will help you with Python programming.