python连接数据库SQLServer

python连接数据库SQLServer


连接数据库的几种方式

  • python odbc
  • pymssql

whl如何安装

whl类似于离线安装包,需要使用pip安装。
pip install指令就可以安装。

pymssql连接SQL Server读取中文程序“UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)”

原因
python默认使用unicode编码,编码不一致问题导致。
解决办法
找到python安装目录下Lib目录,site.py文件,找到setencoding函数
if 0:
# Enable to support locale aware default string encodings.
修改"if 0"为"if 1"就可以了

    def setencoding():
    """Set the string encoding used by the Unicode implementation.  The
    default is 'ascii', but if you're willing to experiment, you can
    change this."""
    encoding = "ascii" # Default value set by _PyUnicode_Init()
    if 0:
        # Enable to support locale aware default string encodings.
        import locale
        loc = locale.getdefaultlocale()
        if loc[1]:
            encoding = loc[1]
    if 0:
        # Enable to switch off string to Unicode coercion and implicit
        # Unicode to string conversion.
        encoding = "undefined"
    if encoding != "ascii":
        # On Non-Unicode builds this will raise an AttributeError...
        sys.setdefaultencoding(encoding) # Needs Python Unicode build !
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容