Home > Database > Mysql Tutorial > python向mysql写下时出现中文乱码

python向mysql写下时出现中文乱码

WBOY
Release: 2016-06-07 16:23:58
Original
1195 people have browsed it

python向mysql写入时出现中文乱码 http://down.chinaz.com/server/201111/1423_1.htm 1.先把数据库的配置全设置为utf8. mysql语句: show variables like '%char%'; 在显示结果中,哪些不是utf8的,全部通过命令: set variable_name = utf8; ?来设置为utf8格

python向mysql写入时出现中文乱码

http://down.chinaz.com/server/201111/1423_1.htm

1.先把数据库的配置全设置为utf8. mysql语句:

show variables like '%char%';
Copy after login

在显示结果中,哪些不是utf8的,全部通过命令:

set variable_name = utf8;
Copy after login

?来设置为utf8格式。

?

2.然后是在Python代码中修改连接语句:

    conn = MySQLdb.connect(host='localhost', user='root',passwd='123', db='account', charset='utf8')  # OK,如果没有charset='utf8',插入为乱码
Copy after login

?

3.个性python的首行

# -*- coding:utf8 -*-
Copy after login

?

如果从数据库中读取的中文输出到网页,如果没有任何内容显示,加入以下代码可解决:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
Copy after login

?

?

另外这偏文章对各编码也有帮助:

http://down.chinaz.com/server/201111/1423_1.htm

?

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template