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
相关产品推荐
相关产品推荐

