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 汉字乱码的解决方案 的介绍,望你能有所收获。

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor
