Windows系统

使用 pymssql 死活没连接成功, 于是改为 pyodbc 连接.

步骤:

  1. 安装 pyodbc

    https://pypi.org/project/pyodbc/
    
  2. 安装 DBUtils

    https://pypi.org/project/DBUtils/
    
  3. 安装 odbc-driver

    https://docs.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-2017
    
  4. 测试代码如下:

    #coding=utf-8
    import pymssql
    import pyodbc
    from DBUtils.PersistentDB import PersistentDB
    
    server = "xxx.database.chinacloudapi.cn"
    user = "user"
    password = "xxx34J#xxb"
    db = "xxxdb"
    # ODBC Driver 版本数字要对应
    persist = PersistentDB(creator=pyodbc, DRIVER="{ODBC Driver 17 for SQL Server}",
     PORT="1433", SERVER=server, DATABASE=db, UID=user, PWD=password)
    
    #查看连接是否成功
    conn = persist.connection()
    cursor = conn.cursor()
    
    sql = 'select * from mytable'
    cursor.execute(sql)
    rs = cursor.fetchall()
    print(rs)