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 !
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容