search
HomeDatabaseMysql Tutorialpython页面乱码问题及解决(pycharm+mysql)

Ubuntu下: there is no doubt that 这是编码问题引起的,应该确保mysql,pycharm和浏览器的编码三码合一。 1,在每个python页开头加上一行“# -*- coding: utf-8 -*-” 类这样:#!/usr/bin/python # -*- coding: utf-8 -*- 2,修改pycharm的编码: setting -

Ubuntu下:

there is no doubt that 这是编码问题引起的,应该确保mysql,pycharm和浏览器的编码三码合一。


1,在每个python页开头加上一行“# -*- coding: utf-8 -*-”

     类似这样:#!/usr/bin/python

                          # -*- coding: utf-8 -*-

2,修改pycharm的编码:

setting -> File Encoding -> 所有 编码方式都改成UTF-8(网上一些教程说IDE Encoding要改成GBK,是因为在windows下默认编码是GBK,而ubuntu下是utf8)


3,修改mysql的编码:

     查看你的mysql编码:登录后,输入命令show variables like '%char%' 如:mysql>show variables like '%char%' 然后就会显示mysql的默认编码:

     可以手动用命令set @@character_set_client=utf8来修改,但是似乎不是永久的,建议直接修改配置文件/etc/mysql/my.cnf文件

     找到[client]和[mysql]在它们下面加入这句default-character-set=utf8,

    找到[mysqld],在它下面加这段

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

保存退出。

重启mysql。在shell命令行下输入sudo service mysql restart

重启后登录mysql查看编码(命令show variables like '%char%'),会发现都变成了utf-8,除了filesystem(我的character_set_database还是GBK,不过似乎没什么影响)

网上找到的修改mysql配置文件的教程都是说在[mysqld]下加入default-character-set=utf8和init_connect='SET NAMES utf8'这两句,但是实践证明加入后无法重启mysql。读者不妨可以试试。


4,修改浏览器编码。ubuntu下一般是用火狐浏览器,修改方法如下:

    编辑 -》首选项 -》内容 -》字体和颜色的“高级”按钮 -》默认字符编码改为UTF8


补充一点:python连接数据库时要加上一句charset='utr8'
例如:

conn = MySQLdb.connect(host='localhost',user='root',passwd='123456',db=‘studentdb',charset='utf8')

这样能解决Python插入Mysql后编程乱码的问题。


如果以上都保证无误,还是出现乱码问题,可以尝试检查:

1,火狐浏览器页面上,右键查看页面信息 -》常规 -》看看编码是不是utf8,如果不是的话:在火狐浏览器左上角 :查看-》字符编码 -》utf8


2,如果页面字符编码是utf8,应确保mysql里存的数据不是乱码 : 登录进mysql,查看你的表里的数据是否是乱码,mysql -> select * from yourTableName,如果是乱码,用insert into语句插入中文,后在浏览器看看是否能正常显示。



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor