首页 热点资讯 义务教育 高等教育 出国留学 考研考公

在linux系统下,python连接MYSQL数据库,怎么可以在python中看到中文...

发布网友

我来回答

2个回答

热心网友

#!/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()

热心网友

你是用print输出从数据库中查到的是乱码吗?追问是,乱码,就是用python从数据库里读到的中文都是乱码。

追答你加我HI吧

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com