yong_sun 2018-08-23 02:55 采纳率: 0%
浏览 2097
已结题

python pymssql查询结果显示乱码

1.SQLServer默认是GBK编码,若按如下写法
conn=pymssql.connect(host='.',database='25',charset='GBK')
name='中文'

exe_sql = 'select * from Mir_Hum_Info where sChrName =' + '\'' + name + '\''
查询结果为空[]

2.若修改为 charset='utf8',查询有结果,但有中文的地方显示成 乱码。
然后各种查询了网上的方法,把exe_sql.decode(xxx),encode(YY)
直接报错,没用--

  • 写回答

4条回答 默认 最新

  • yong_sun 2018-08-24 03:36
    关注

    这样就可以,把语句写在 execute() 里,
    cur.execute('select sAccount,sChrName,nLevel from Mir_Hum_Info where sChrName =%s','杰克'.encode('GBK'))

    下边这样就不行,报错 AttributeError: 'tuple' object has no attribute 'encode'
    sql='select sAccount,sChrName,nLevel from Mir_Hum_Info where sChrName =%s','杰克'.encode('GBK')
    cur.execute(sql)

    评论

报告相同问题?