Python MYSQLdb appears gargoyles solution

  • 2020-04-02 14:14:12
  • OfStack

The example of this article describes the Python MYSQLdb appeared messy code solution, to share with you for your reference. Specific methods are as follows:

Generally speaking, the most troublesome problem in using mysql is the garbled code.

View the encoding of mysql:

Command:  

show variables like 'character_set_%';

You can see the following results:

Character_set_client character_set_client
Character_set_connection the encoding used to establish the connection;
character_set_database
Character_set_results result set encoding;
Character_set_server database server encoding;

As long as the above four to ensure that the coding method is the same, there will be no garbled problem .

You can then set the mysql encoding directly here.

set character_set_client = xxxxx

This changes the client code.

However, for the established database and data table, the coding is still unchanged, still want Use the alter command to change the corresponding encoding .

However, even if you change the code of the database, there is still a problem in python with garbled code stored in the database Specify the encoding when linking to the database . Such as:

sql_con = MySQLdb.connect(host=MYSQL_ADDR , user=MYSQL_USER , passwd=MYSQL_PWD , db=MYSQL_DB , charset="utf8")

This specifies that the client code is utf8. The garble problem is then resolved.

I hope this article has helped you with your Python programming.


Related articles: