It is not difficult to solve Chinese Character garbled characters in MySQL Python

Source: Internet
Author: User

The following article describes the correct solution for MySQL Python Chinese Character garbled characters. MySQL Python Chinese Character garbled characters cause a lot of inconvenience in actual operations, the following article describes the correct solution for garbled Chinese Characters in MySQL Python.

1. to implement two classification methods for a second-class text problem, you must first perform word segmentation on the text in the document set and then create an inverted table. The inverted table is based on the MySQL database, however, there is one well-known thing: MySQL inserts Chinese characters in a variety of inexplicable situations. The first thing is that it cannot be inserted, but it is inserted, but the display is garbled, the inserted data is still garbled in the program.

In the final analysis, this is the problem of inconsistent encoding symbols. In the process of solving this problem, I have referred to some website Dongdong. Here I will summarize his messy content so that cainiao like me can get started quickly.

The encoding formats are the same: character_set_client, character_set_connection, character_set_database,

Character_set_results, character_set_server encoding must be consistent. Next we will talk about how to make the three consistent.

1. First, modify the my. ini file in the MySQL installation directory)

(1) modify the my. ini (MySQL Server Instance Configuration file)

 
 
  1. # CLIENT SECTION  
  2. [client]  
  3. port=3306 
  4. [MySQL]  
  5. default-character-set=gbk 
  6. # SERVER SECTION  
  7. [MySQLd]  
  8. default-character-set=gbk   

Then modify character_set_server, which is the most troublesome and critical. The specific method is as follows:

Turn off the service first,

DOS-MySQL installation directory, bin

MySQLadmin-u root-p shutdown (return). Then, the system will prompt you to transfer the password.

The root privilege of MySQL is required to stop MySQL Data migration.

Then MySQLd-c gbk starts the server.-C is a shorthand for character_set_server. You can also write it as MySQLd-character_set_server gbk ).

Next, create a new database using the following method: MySQLCommandLineClient:

 
 
  1. create database yourDB CHARACTER SET gbk;  
  2. set names 'gbk'  

Restart and shut down the MySQL service. After restarting, you can insert Chinese characters.

After completing the preceding operations, enter use nlpprocess under MySQLCommandLineClient; (Note: My new database)

Show variables like 'character % 'will show the graph at the beginning of the blog, which means the character set settings are consistent.

Next let's take a look at the database display after the modification. My system is MySQL5.1 + administrator + querybrowser)

The data in is added by C # to MySQL. Note that the link string should be constructed as follows,

 
 
  1. string conmyengine="server=localhost;user=root;database=nlpprocess;port=3306;password=xxxx;charset=gbk"; 

The following describes how to set up MySQL Python to read Chinese Character fields without garbled characters.

MySQL Python reads forms with Chinese character values

 
 
  1. import MySQLdb  
  2. db=MySQLdb.connect(user='root',db='nlpprocess',passwd='xxx',host='localhost')  
  3. cursor=db.cursor()  
  4. select='select term_id,stemroot from nlpinvert where term_id between 10 and 14' 
  5. cursor.execute("set NAMES GBK")  
  6. cursor.execute(select)  
  7. for r in cursor.fetchall():  
  8. print str(r[0])+':'+r[1]  
  9. cursor.close()  
  10. db.close()  

The result is as follows:


The above content is an introduction to the MySQL Python Chinese Character garbled solution. I hope you will have some gains.

Related Article

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.