V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wuxiaolin
V2EX  ›  Python

pymssql 去链接sql server,中文乱码,求助?

  •  
  •   wuxiaolin · 2013-07-23 16:49:23 +08:00 · 5915 次点击
    这是一个创建于 3920 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近搞个东西需要去sql server拿数据过来做汇总,发现从那么拿过来的数据中文都是直接问号显示,但奇怪的事我本机win系统下是正常的,到了我服务器上面(linux)就乱套了,读出来的数据只要是中文的,就全部都是问号显示,请问这是pymssql 的问题还是服务器问题?有什么方法解决么
    9 条回复    1970-01-01 08:00:00 +08:00
    loading
        1
    loading  
       2013-07-23 17:01:04 +08:00 via iPhone
    在SQL server开跟踪,看查询语句是否有乱码。
    我在windows下连的,编码搞得郁闷,各种encode,decode。
    loading
        2
    loading  
       2013-07-23 17:02:51 +08:00 via iPhone
    我选用了adodbapi
    xzl
        3
    xzl  
       2013-07-23 17:24:20 +08:00
    指定连接编码为UTF-8即可。
    wuxiaolin
        4
    wuxiaolin  
    OP
       2013-07-23 17:36:37 +08:00
    @xzl 这东西挺怪的,我在win上面跑,设置charset='utf8' 后,只能读到utf8的数据,中文都读不到,当时真的就各种冷汗
    wuxiaolin
        5
    wuxiaolin  
    OP
       2013-07-23 17:42:18 +08:00
    刚调试过linux的,设置成utf8之后,只能读到非中文的数据
    lerry
        6
    lerry  
       2013-07-23 17:52:40 +08:00
    我用过一段,如果直接print结果的话,文件设置
    # -*- coding: gb2312 -*-
    连接不指定字符

    如果结果别处保存的话
    # -*- coding: utf-8 -*-
    con = pymssql.connect(host='xxxx', user='sa', password='xxxx', database='xxx',charset='utf8')

    我记得在Linux上试过,也是没有问题的
    est
        7
    est  
       2013-07-23 17:55:53 +08:00
    @wuxiaolin

    把这行SQL语句结果贴出来吧:

    show variables like '%char%';
    wuxiaolin
        8
    wuxiaolin  
    OP
       2013-07-23 17:56:23 +08:00
    啊啊,蛋疼,原来是我程序里面过滤了那些数据
    wuxiaolin
        9
    wuxiaolin  
    OP
       2013-07-23 17:57:21 +08:00
    @est
    @lerry
    @xzl
    @loading 感谢这么多位啊,是我过滤了那些数据,之前一直以为是charset问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1031 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:31 · PVG 03:31 · LAX 12:31 · JFK 15:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.