MySQL python 汉字乱码的最终解决方案
以下的文章主要讲述的是MySQL python 汉字乱码的解决方案 ,我前两天在相关网站看见MySQL python 汉字乱码的解决方案的资料,觉得挺好,就拿出来供大家分享。希望会给你带来一些帮助在此方面。 1.想实现对一个二类文本问题的两种分类方法,首先要对文档集合
以下的文章主要讲述的是MySQL python 汉字乱码的解决方案 ,我前两天在相关网站看见MySQL python 汉字乱码的解决方案的资料,觉得挺好,就拿出来供大家分享。希望会给你带来一些帮助在此方面。
1.想实现对一个二类文本问题的两种分类方法,首先要对文档集合的文本进行分词,然后建立倒排表,倒排表是借助于MySQL数据库的,但是有一个众所周知的事情,MySQL插入汉字会出现各种莫名奇妙的情况,首当其冲是插不进去,而获是插进去了,但是显示是乱码,然后将插入后的数据再读到程序中仍然是乱码。
这归根结底是编码符号不一致的问题也就是。再解决这个问题的过程中,我参考了的东东,这里对他凌乱的内容加以总结,让和我一样的菜鸟能够快速入门。
我们所说的编码格式一致:
character_set_client,character_set_connection,character_set_database,
character_set_results,character_set_server编码要一致。下面说说如何将这三者调为一致。
1.首先,修改my.ini文件(该文件在MySQL安装目录下)
(1)修改 my.ini(MySQL Server Instance Configuration 文件)
<ol class="dp-xml"> <li class="alt"><span><span># CLIENT SECTION </span></span></li> <li><span>[client] </span></li> <li class="alt"> <span class="attribute">port</span><span>=</span><span class="attribute-value">3306</span><span> </span> </li> <li> <span>[</span>MySQL<span>] </span> </li> <li class="alt"> <span class="attribute">default-character-set</span><span>=</span><span class="attribute-value">gbk</span><span> </span> </li> <li><span># SERVER SECTION </span></li> <li class="alt"> <span>[</span>MySQL<span>d] </span> </li> <li> <span class="attribute">default-character-set</span><span>=</span><span class="attribute-value">gbk</span><span> </span> </li> </ol>
MySQL python 汉字乱码的解决方案后修改character_set_server 这个是最麻烦的,也是最关键的,具体方法如下:
先关掉服务,
DOS-》MySQL安装目录,bin
MySQLadmin -u root -p shutdown (回車)然後系統將提示輸入密碼
因為停止MySQL數據庫是需要MySQL的root權限的
然后MySQLd –C GBK启动服务器 –C是character_set_server的一个简写(可以也可以写成MySQLd –character_set_server gbk).
其次,创建新数据库,方法如下:MySQLCommandLineClient下:
<ol class="dp-xml"> <li class="alt"><span><span>create database yourDB CHARACTER SET gbk; </span></span></li> <li><span>set names 'gbk' </span></li> </ol>
然后重启将MySQL 服务关掉,再重启后就可以插入汉字了
完成以上操作后,在MySQLCommandLineClient下输入 use nlpprocess;(注:我新建的数据库)
show variables like 'character%'就会出现博文开头的图,也就说明字符集设置一致了。
下面我们看看修改后数据库显示情况(我的系统为MySQL5.1+administrator+querybrowser)
上图中的数据是C#向MySQL中加入的,需要注意的是链接字符串应该如下构造,
stringconmyengine="server=localhost;user=root;database=nlpprocess;port=3306;password=xxxx;charset=gbk";
下面说下python如何设置才能正确无误,没有乱码地从数据库中读取汉字字段。
python读取有汉字值的表单
<ol class="dp-xml"> <li class="alt"><span>import MySQLdb </span></li> <li> <span class="attribute">db</span><span>=</span>MySQL<span class="attribute-value">db</span><span>.connect(</span><span class="attribute">user</span><span>=</span><span class="attribute-value">'root'</span><span>,</span><span class="attribute">db</span><span>=</span><span class="attribute-value">'nlpprocess'</span><span>,</span><span class="attribute">passwd</span><span>=</span><span class="attribute-value">'xxx'</span><span>,</span><span class="attribute">host</span><span>=</span><span class="attribute-value">'localhost'</span><span>) </span> </li> <li class="alt"> <span class="attribute">cursor</span><span>=</span><span class="attribute-value">db</span><span>.cursor() </span> </li> <li> <span class="attribute">select</span><span>=</span><span class="attribute-value">'select term_id,stemroot from nlpinvert where term_id between 10 and 14'</span><span> </span> </li> <li class="alt"><span>cursor.execute("set NAMES GBK") </span></li> <li><span>cursor.execute(select) </span></li> <li class="alt"><span>for r in cursor.fetchall(): </span></li> <li><span>print str(r[0])+':'+r[1] </span></li> <li class="alt"><span>cursor.close() </span></li> <li><span>db.close() </span></li> </ol>
以上的相关内容就是对MySQL python 汉字乱码的解决方案 的介绍,望你能有所收获。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Discussion on Hierarchical Structure in Python Projects In the process of learning Python, many beginners will come into contact with some open source projects, especially projects using the Django framework...

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...
