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)
- # CLIENT SECTION
- [client]
- port=3306
- [MySQL]
- default-character-set=gbk
- # SERVER SECTION
- [MySQLd]
- 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:
- create database yourDB CHARACTER SET gbk;
- 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,
- 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
- import MySQLdb
- db=MySQLdb.connect(user='root',db='nlpprocess',passwd='xxx',host='localhost')
- cursor=db.cursor()
- select='select term_id,stemroot from nlpinvert where term_id between 10 and 14'
- cursor.execute("set NAMES GBK")
- cursor.execute(select)
- for r in cursor.fetchall():
- print str(r[0])+':'+r[1]
- cursor.close()
- 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.