今天尝试着用 Python 写了个脚本试着连接 mysql 数据库,并查询里边的数据,不过最终查询结果中文字符变成了ascii格式。

代码如下:

#!/usr/bin/python
#encoding=utf-8 import MySQLdb
import json db = MySQLdb.connect(host='xxx.xxx.xx.xxx',port=3306,user='name',passwd='pwd',db='my_database_name')
cursor = db.cursor()
sql = "select * from platform_temp"
aa=cursor.execute(sql)
info = cursor.fetchmany(aa)
for i in info:
print i
db.close()

  

查询结果如下:

针对上述出现的问题,对编辑器Pycharm的环境都进行了设置为utf-8格式,但是结果还都如上图所示。

最后通过查询本地window控制台字节码格式,为ascii,如下图所示:

In[5]: import sys
In[6]: print(sys.getdefaultencoding())
ascii

  

再次对上述代码进行修改,结果还是如此,代码如下:

#!/usr/bin/python
#encoding=utf-8 import MySQLdb
import json db = MySQLdb.connect(host='xxx.xxx.xxx.xxx',port=3306,user='name',passwd='pwd',db='my_database_name')
cursor = db.cursor()
sql = "select * from platform_temp"
aa=cursor.execute(sql)
info = cursor.fetchmany(aa)
for i in info:
print str(i).encode('utf-8')
print str(i).decode('utf-8')
print str(i).decode('utf-8').encode('utf-8')
db.close()

  

最后通过尝试将  json  模块导入,利用其 dumps 方法,问题得到解决,代码如下图所示:

#!/usr/bin/python
#encoding=utf-8 import MySQLdb
import json db = MySQLdb.connect(host='xxx.xxx.xxx.xxx',port=3306,user='name',passwd='pwd',db='my_database_name')
cursor = db.cursor()
sql = "select * from platform_temp"
aa=cursor.execute(sql)
info = cursor.fetchmany(aa)
for i in info:
# print str(i).encode('utf-8')
# print str(i).decode('utf-8')
# print str(i).decode('utf-8').encode('utf-8')
print json.dumps(i, encoding='UTF-8', ensure_ascii=False)

  

查询结果如下所示:

问题解决,结束。

实例配置:

    def get_one(self):
#准备sql
sql = 'SELECT * FROM `news` WHERE `types` = %s ORDER BY `created_at` DESC;'
# 找到cursor
cursor = self.conn.cursor()
cursor.execute('SET NAMES UTF8')
# 执行sql
cursor.execute(sql, ('百家', ))
# 拿到结果
rest = cursor.fetchmany()
for r in rest:
print json.dumps(r, encoding='UTF-8', ensure_ascii=False)
# 处理数据
cursor.close()
self.close_conn()

  

[root@centos-01 python]# python test001.py
[2, "男子长得像\"祁同伟\"挨打 打人者:为何加害检察官", "新闻内容", "百家", "/static/img/news/02.png", null, 0, null, 1]

  python3 可以用 pymysql 连接数据库 , 没有中文乱码的问题

解决Python查询Mysql数据库信息乱码问题的更多相关文章

  1. 解决Python向MySQL数据库插入中文数据时出现乱码

    解决Python向MySQL数据库插入中文数据时出现乱码 先在MySQL命令行中输入如下语句查看结果: 只要character_set_client character_set_database ch ...

  2. 解决Python查询Mysql中文乱码问题

    前段时间,自己瞎动手用Django写了一个更新zip包和sql到远程服务器的工具.但Python从Mysql中读取出来的中文字符会乱码,如下图: 解决办法:Python连接Mysql时指定charse ...

  3. 解决navicate 连接mysql数据库中文乱码的问题

    以下均是ubuntu12.04为准 1.修改mysql的配置文件. 1.1.vi  /etc/mysql/my.conf找到[client]在其下面添加 default-character-set=u ...

  4. 解决WampServer中MySQL数据库中文乱码的问题

    原文地址:http://blog.csdn.net/qq756703833/article/details/37971057 左键点击托盘区的WampServer图标,选择MySQL--my.ini, ...

  5. MySQL中 如何查询表名中包含某字段的表 ,查询MySql数据库架构信息:数据库,表,表字段

    --查询tablename 数据库中 以"_copy" 结尾的表 select table_name from information_schema.tables where ta ...

  6. Python与Mysql 数据库的连接,以及查询。

    python与mysql数据库的连接: pymysql是python中对数据库的连接模块:因此应当首先安装pymysql数据库模块. 执行pip install pymysql 命令. 然后在pyth ...

  7. Django 连接mysql数据库中文乱码

    Django 连接mysql数据库中文乱码 2018年08月25日 20:55:15 可乐乐乐乐乐 阅读数:566   版本:CentOS6.8 python3.6.4 django1.8.2 数据库 ...

  8. python使用mysql数据库

    一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的l ...

  9. python专题-Mysql数据库(python2._+ Mysqldb)

    Python使用MySQL数据库 MySQLdb驱动从2014年1月停止了维护. Python2 MySQLdb 一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安 ...

随机推荐

  1. selenium+python自动化99-清空输入框clear()失效问题解决

    前言 在使用selenium做UI自动化的时候,发现有些弹出窗上的输入框,输入文本后,使用clear()方法无效. 这样会导致再次输入时,字符串不是清空后输入,而是跟着后面输入一长串,导致结果不准. ...

  2. [转] C++ explicit关键字详解

    本文转自tiankong19999 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的, 而非隐式的, 跟它相对应的另一个关键字是impl ...

  3. 项目Alpha冲刺(团队)-第九天冲刺

    格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队) 团队名称:为了交项目干杯 作业目标:描述第九天冲刺的项目进展.问题困难.心得体会 队员姓名与学号 队员学号 ...

  4. postgres高可用学习篇三:haproxy+keepalived实现postgres负载均衡

    环境: CentOS Linux release 7.6.1810 (Core) 内核版本:3.10.0-957.10.1.el7.x86_64 node1:192.168.216.130 node2 ...

  5. 修改idea,webstrom,phpstrom 快捷键double shift 弹出search everywhere

    这个问题很困惑,因为这个功能很好用,查找什么很方便,but! 我用了十年的搜狗输入法,大家都知道搜狗输入法按shift中英文切换很方便,特别在写代码时候...所以就和这个double shift功能冲 ...

  6. Python垃圾回收机制?

    Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾.在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的 ...

  7. Problem I. Wiki with Special Poker Cards

    Problem I. Wiki with Special Poker CardsInput file: standard input Time limit: 1 secondOutput file: ...

  8. wiki with 35(dp+矩阵快速幂)

    Problem J. Wiki with 35Input file: standard input Time limit: 1 secondOutput file: standard output M ...

  9. c++ main函数

    vs 2015的运行环境 1.参数 int main(int argc, char* argv[]) 1)两个参数的类型是固定的,但参数名可以是符合命名规则的任何命名 2)argv[0]为执行文件的路 ...

  10. My journey introducing the data build tool (dbt) in project’s analytical stacks

    转自:https://www.lantrns.co/my-journey-introducing-the-data-build-tool-dbt-in-projects-analytical-stac ...