MACOS 系统使用
TzyTman opened this issue · 4 comments
import pyodbc
class SqlTest():
def init(self):
# 连接数据库
server = 'localhost'
database = 'SQL'
username = 'SA'
password = 'SQLsqlserver1234'
port = '1433'
# 使用ODBC驱动连接SQL Server
conn_str = f'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server},{port};DATABASE={database};UID={username};PWD={password}'
self.conn = pyodbc.connect(conn_str)
# 创建游标对象
self.cursor = self.conn.cursor()
def execute_query(self):
# 执行查询
self.cursor.execute('SELECT * FROM dbo.Customers')
# 获取查询结果
rows = self.cursor.fetchall()
for row in rows:
print(row)
def close_connection(self):
# 关闭连接
self.conn.close()
def main():
print("进入主应用程序")
sql_test = SqlTest()
sql_test.execute_query()
sql_test.close_connection()
if name == "main":
main()
报错:
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
Check that the DM can find the driver, and that its dependencies are also available. You've given next to no information about your environment but if you're using macOS then run otool -L
on the driver library to check its dependencies.
Also, this is an English project.
You can, but it won't help much if no one understands what you're trying to say.