A workaround for garbled mysqldb in Python

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.