You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS下Python连接MSSQL提示ODBC Driver 17未找到求助

MacOS下Python连接MSSQL数据库报错求助

我在MacOS系统上用Python连接MSSQL数据库时,一直碰到以下错误:

DBAPIError: (pyodbc.Error) ('01000', "[01000] [unixODBC][Driver Manager]'Cannot open lib 'ODBC Driver 17 for SQL Server': file not found (0) (SQLDriverConnect)")

我已经按照微软官方提供的MacOS安装ODBC驱动的步骤操作过,但问题仍未解决,有没有遇到过这个问题的朋友能帮忙看看?

我的代码如下:

import pyodbc as po
import pandas as pd
from sqlalchemy import create_engine

server = "localhost"
db = "master"
user = **
pwd = **

driver_name = ''
driver_names = [x for x in pyodbc.drivers()]
if driver_names:
    driver_name = driver_names[0]


# Connection string
cnxn = po.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=' +
        server+';DATABASE='+db+';UID='+user+';PWD=' + pwd)
cursor = cnxn.cursor()
 
# Fetch data into a cursor
cursor.execute("SELECT TOP (10) PersonID, FullName, PhoneNumber, \
    EmailAddress FROM WideWorldImporters.Application.People ORDER BY PersonID DESC;")
 
# iterate the cursor
row = cursor.fetchone()
while row:
    # Print the row
    print(str(row[0]) + ", " + str(row[1] or '') + ", " + str(row[2] or '') + ", " + str(row[3] or ''))
    row = cursor.fetchone()
 
# Close the cursor and delete it
cursor.close()
del cursor
 
# Close the database connection
cnxn.close()

内容的提问来源于stack exchange,提问作者hgyildiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 17:45:17