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 !