在linux系统下,python连接MYSQL数据库,怎么可以在python中看到中文?
在终端用mysql命令查看该数据库时,可以显示中文,但是在python编译后,界面显示??,这个怎么整?网上介绍的什么charset的都试过,没用,python脚本的开头...
在终端用mysql命令查看该数据库时,可以显示中文,但是在python编译后,界面显示??,这个怎么整?网上介绍的什么charset的都试过,没用,python脚本的开头我有添加# -*- coding:utf-8 -*-,求哪位大神给我解答。谢谢!
展开
展开全部
#!/usr/bin/python
# encoding: utf-8
# filename: dbi.py
class DBI:
'''this is a private abstract Database Interface class'''
def __init__(self, **kwg):
self.config = dict(kwg)
def open(self):
'''use special driver to connect RDBMS and generate cursor'''
pass
def close(self):
self.curr.close()
self.conn.close()
self.curr = None
self.conn = None
def __enter__(self):
self.open()
return self
def __exit__(self, *args):
self.close()
...
#!/usr/bin/python
# encoding: utf-8
# filename: mydbi.py
import MySQLdb
import dbi
class MyDBI(DBI):
def open(self):
self.conn = MySQLdb.connect(
host = self.config.get('host','localhost'),
user = self.config.get('user','root'),
passwd = self.config.get('passwd',''),
db = self.config.get('db','mysql'),
charset = self.config.get('charset','utf8'),
use_unicode = self.config.get('use_unicode',True),
)
self.curr = self.conn.cursor()
# encoding: utf-8
# filename: dbi.py
class DBI:
'''this is a private abstract Database Interface class'''
def __init__(self, **kwg):
self.config = dict(kwg)
def open(self):
'''use special driver to connect RDBMS and generate cursor'''
pass
def close(self):
self.curr.close()
self.conn.close()
self.curr = None
self.conn = None
def __enter__(self):
self.open()
return self
def __exit__(self, *args):
self.close()
...
#!/usr/bin/python
# encoding: utf-8
# filename: mydbi.py
import MySQLdb
import dbi
class MyDBI(DBI):
def open(self):
self.conn = MySQLdb.connect(
host = self.config.get('host','localhost'),
user = self.config.get('user','root'),
passwd = self.config.get('passwd',''),
db = self.config.get('db','mysql'),
charset = self.config.get('charset','utf8'),
use_unicode = self.config.get('use_unicode',True),
)
self.curr = self.conn.cursor()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是用print输出从数据库中查到的是乱码吗?
追问
是,乱码,就是用python从数据库里读到的中文都是乱码。
追答
你加我HI吧
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询