mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.88k stars 561 forks source link

MACOS 系统使用 #1336

Closed TzyTman closed 3 months ago

TzyTman commented 3 months ago

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)")

v-chojas commented 3 months ago

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.

TzyTman commented 3 months ago

Who said that! You can't speak Chinese in English projects?

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: v-chojas @.> 发送时间: 2024年3月22日 22:41 收件人: mkleehammer/pyodbc @.> 抄送: tuzhengyao @.>, Author @.> 主题: Re: [mkleehammer/pyodbc] MACOS 系统使用 (Issue #1336)

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.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

v-chojas commented 3 months ago

You can, but it won't help much if no one understands what you're trying to say.

TzyTman commented 3 months ago

Thank you for your advice. I'll make the question clearer in the future.

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: v-chojas @.> 发送时间: 2024年3月22日 23:28 收件人: mkleehammer/pyodbc @.> 抄送: tuzhengyao @.>, Author @.> 主题: Re: [mkleehammer/pyodbc] MACOS 系统使用 (Issue #1336)

You can, but it won't help much if no one understands what you're trying to say.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>