python连接sqlserver_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 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值