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 !