python 查询数据库,后中文乱码

# -*- coding: utf-8 -*-
import pymssql


# Create your views here.
# 数据库服务器信息
server = "localhost"
user = "sa"
password = "hzp"
database = "test1"
charset = 'utf-8'

class Data:

    def getdata(sql, ele1='', ele2=''):
        # get connection
        conn = pymssql.connect(server, user, password, database, charset)

        # var cursor
        cur = conn.cursor()

        # get data
        cur.execute(sql, ele1)

        rows = cur.fetchall()

        cur.close()
        conn.close()

        return rows

sql = u'select top 1 description from fruit where  description is not null '
data = Data.getdata(sql)
data2 = data[0][0]
print(data2)
# print(data[0][0].encode('utf-8'))

# # string.strip() :trim the ' ' in strings
# print(data[0][1].strip())

猜你喜欢

转载自blog.csdn.net/hzp666/article/details/86690914
今日推荐