之前用pymssql 换个python版本就运行不了
换了pyodbc 并且指定odbc diver 就好了
风波老师指出原因 需要指定odbc driver
#!/usr/bin/env python
# coding: utf-8
import pyodbc
import pandas as pd
statement =
# 创建连接字符串
conn_str =
# 创建连接
con = pyodbc.connect(conn_str)
df = pd.read_sql(statement, con)
print(df)
创建连接字符串:
格式化字符串避免干扰
server = "SERVER"
database = "DB"
uid = "UID"
pwd = "PWD"
# 方法1 分句使用fr
conn_str = (
r'DRIVER={ODBC Driver 17 for SQL Server};'
fr'SERVER={server};'
fr'DATABASE={database};'
fr'UID={uid};'
fr'PWD={pwd};'
)
conn_str
server = "SERVER"
database = "DB"
uid = "UID"
pwd = "PWD"
# 方法2 fr+{{}}
conn_str = (
fr'DRIVER={{ODBC Driver 17 for SQL Server}};'
fr'SERVER={server};'
fr'DATABASE={database};'
fr'UID={uid};'
fr'PWD={pwd};'
)
conn_str
# 方法3 {{}}+format
conn_str = (
r'DRIVER={{ODBC Driver 17 for SQL Server}};'
r'SERVER={0};'
r'DATABASE={1};'
r'UID={2};'
r'PWD={3};'
).format(*list_config)
标签:fr,UID,SERVER,PWD,str,pymssql,pyodbc,mssql,conn
From: https://blog.51cto.com/u_16055028/8786437