{"id":685,"library":"pyodbc","title":"pyodbc: Python ODBC Bridge","description":"pyodbc is an open-source Python module that provides a simple and consistent interface for connecting to various databases via ODBC (Open Database Connectivity), implementing the DB API 2.0 specification. It allows for executing SQL queries, retrieving results, and managing database operations efficiently. As of version 5.3.0, it supports Python 3.9 through 3.14 and is actively maintained with frequent releases.","status":"active","version":"5.3.0","language":"python","source_language":"en","source_url":"https://github.com/mkleehammer/pyodbc","tags":["database","odbc","sql","db-api"],"install":[{"cmd":"pip install pyodbc","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"pyodbc","correct":"import pyodbc"},{"symbol":"connect","correct":"cnxn = pyodbc.connect('DSN=your_dsn;UID=user;PWD=pass')"}],"quickstart":{"code":"import pyodbc\nimport os\n\n# NOTE: This example requires an ODBC driver and DSN to be configured on your system.\n# For Windows, common drivers like 'SQL Server' might be available.\n# For Linux/macOS, you might need to install unixODBC and specific database drivers.\n# Example DSN-less connection string (replace with your actual details):\nDB_DRIVER = os.environ.get('PYODBC_DRIVER', '{ODBC Driver 17 for SQL Server}')\nDB_SERVER = os.environ.get('PYODBC_SERVER', 'your_server.database.windows.net')\nDB_DATABASE = os.environ.get('PYODBC_DATABASE', 'your_database_name')\nDB_UID = os.environ.get('PYODBC_UID', 'your_username')\nDB_PWD = os.environ.get('PYODBC_PWD', 'your_password')\n\nconnection_string = (\n    f\"DRIVER={DB_DRIVER};\"\n    f\"SERVER={DB_SERVER};\"\n    f\"DATABASE={DB_DATABASE};\"\n    f\"UID={DB_UID};\"\n    f\"PWD={DB_PWD};\"\n)\n\ntry:\n    cnxn = pyodbc.connect(connection_string)\n    cursor = cnxn.cursor()\n\n    # Example: Create a table (if it doesn't exist)\n    try:\n        cursor.execute(\"CREATE TABLE #TestTable (id INT, name VARCHAR(50))\")\n        print(\"Table #TestTable created.\")\n    except pyodbc.ProgrammingError as e:\n        if 'There is already an object named' in str(e): # For SQL Server temp table\n            print(\"Table #TestTable already exists, skipping creation.\")\n        else:\n            raise\n\n    # Example: Insert data\n    cursor.execute(\"INSERT INTO #TestTable (id, name) VALUES (?, ?)\", 1, 'Alice')\n    cursor.execute(\"INSERT INTO #TestTable (id, name) VALUES (?, ?)\", 2, 'Bob')\n    cnxn.commit() # Commit changes if autocommit is not enabled\n    print(\"Data inserted.\")\n\n    # Example: Select data\n    cursor.execute(\"SELECT id, name FROM #TestTable\")\n    rows = cursor.fetchall()\n    print(\"\\nFetched Data:\")\n    for row in rows:\n        print(f\"ID: {row.id}, Name: {row.name}\")\n\nexcept pyodbc.Error as ex:\n    sqlstate = ex.args[0]\n    print(f\"Database Error (SQLSTATE: {sqlstate}): {ex}\")\nfinally:\n    if 'cnxn' in locals() and cnxn:\n        cnxn.close()\n        print(\"\\nConnection closed.\")","lang":"python","description":"This quickstart demonstrates how to establish a connection to an ODBC database using `pyodbc`, execute SQL commands, insert data, and retrieve results. It uses environment variables for connection details and requires an appropriate ODBC driver and DSN (or DSN-less connection string) to be pre-configured on your system."},"warnings":[{"fix":"Upgrade to Python 3.x and pyodbc 5.x, or stay on pyodbc 4.x for Python 2.7.","message":"pyodbc 5.x dropped support for Python 2.x. If you are on an older Python 2 environment, you must use pyodbc 4.x or migrate to Python 3.","severity":"breaking","affected_versions":"<5.0.0"},{"fix":"Upgrade your Python environment to 3.9 or later, or use pyodbc < 5.3.0 for Python 3.8.","message":"pyodbc 5.3.0 dropped support for Python 3.8, requiring Python 3.9 or newer.","severity":"breaking","affected_versions":"5.3.0+"},{"fix":"Refer to `pyodbc` documentation or your database vendor's documentation for installing the necessary ODBC drivers and driver manager for your operating system.","message":"pyodbc is a bridge to ODBC drivers. You *must* install an appropriate ODBC driver manager (e.g., unixODBC on Linux/macOS) and specific database ODBC drivers (e.g., SQL Server ODBC Driver) on your system for pyodbc to function. This is an external, operating-system-level dependency, not a Python package dependency.","severity":"gotcha","affected_versions":"All"},{"fix":"Call `cnxn.commit()` after data modification statements or initialize connection with `pyodbc.connect(..., autocommit=True)`.","message":"Transactions are not automatically committed by default. You must explicitly call `connection.commit()` after `INSERT`, `UPDATE`, or `DELETE` operations, or pass `autocommit=True` to `pyodbc.connect()` if you want each statement to commit immediately.","severity":"gotcha","affected_versions":"All"},{"fix":"Upgrade to pyodbc 5.0.1 or later.","message":"Version 5.0.0 introduced a bug preventing `bytes` objects from being passed in the `attrs_before` parameter of `pyodbc.connect()`, commonly used for Azure authentication tokens. This was fixed in 5.0.1.","severity":"gotcha","affected_versions":"5.0.0"},{"fix":"Upgrade to pyodbc 5.1.0 or later for stable macOS ARM support.","message":"macOS ARM (Apple Silicon M1/M2) users experienced issues with precompiled binaries in early 5.x releases. Dedicated Mac ARM wheels were introduced and stabilized in 5.1.0.","severity":"gotcha","affected_versions":"5.0.0 - 5.0.1"}],"env_vars":null,"last_verified":"2026-05-12T17:48:51.144Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"pip install pyodbc","cause":"The pyodbc library is not installed or is not available in the current Python environment.","error":"ModuleNotFoundError: No module named 'pyodbc'"},{"fix":"Install the appropriate ODBC driver for your database (e.g., ODBC Driver 17/18 for SQL Server) and ensure its name in the connection string is correct.","cause":"The specified ODBC driver is not installed on the system, is incorrectly named, or is not correctly configured.","error":"pyodbc.OperationalError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')"},{"fix":"Verify that the username and password in your connection string are correct and that the user has appropriate database access permissions.","cause":"The provided username or password in the connection string is incorrect, or the user lacks the necessary database permissions.","error":"pyodbc.OperationalError: ('28000', '[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user \\'your_username\\'. (18456) (SQLDriverConnect)')"},{"fix":"Ensure the `pyodbc.connect()` call succeeds by handling potential exceptions or verifying the connection object is not `None` before creating a cursor.","cause":"The database connection failed to establish, resulting in a `None` connection object, and you are attempting to call the `.cursor()` method on it.","error":"AttributeError: 'NoneType' object has no attribute 'cursor'"}],"ecosystem":"pypi","meta_description":null,"install_score":0,"install_tag":"stale","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":"5.3.0","install_checks":{"last_tested":"2026-05-12","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"22.1M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.6,"import_time_s":null,"mem_mb":null,"disk_size":"19M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"23.9M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.6,"import_time_s":null,"mem_mb":null,"disk_size":"21M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"15.8M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":null,"mem_mb":null,"disk_size":"13M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"15.6M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":null,"mem_mb":null,"disk_size":"13M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"21.6M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.8,"import_time_s":null,"mem_mb":null,"disk_size":"19M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}}