🔍 Code Extractor

function test_odbc_driver

Maturity: 42

Tests the availability of ODBC drivers on the system, specifically checking for SQL Server drivers and returning a boolean indicating whether SQL Server drivers are found.

File:
/tf/active/vicechatdev/full_smartstat/test_odbc.py
Lines:
10 - 29
Complexity:
simple

Purpose

This diagnostic function verifies that the system has ODBC drivers installed and accessible through pyodbc. It's primarily used for environment validation, troubleshooting database connectivity issues, and ensuring SQL Server drivers are available before attempting database connections. The function provides user-friendly console output with emoji indicators for success/failure states.

Source Code

def test_odbc_driver():
    """Test ODBC driver availability"""
    print("🔍 Testing ODBC Driver availability...")
    
    try:
        drivers = pyodbc.drivers()
        print(f"✅ Available ODBC drivers: {drivers}")
        
        # Check for SQL Server driver
        sql_server_drivers = [d for d in drivers if 'SQL Server' in d]
        if sql_server_drivers:
            print(f"✅ SQL Server drivers found: {sql_server_drivers}")
            return True
        else:
            print("❌ No SQL Server drivers found")
            return False
            
    except Exception as e:
        print(f"❌ Error checking ODBC drivers: {e}")
        return False

Return Value

Returns a boolean value: True if SQL Server ODBC drivers are found in the system, False if no SQL Server drivers are found or if an exception occurs while checking drivers. The function also prints diagnostic information to the console regardless of the return value.

Dependencies

  • pyodbc

Required Imports

import pyodbc

Usage Example

import pyodbc

def test_odbc_driver():
    """Test ODBC driver availability"""
    print("🔍 Testing ODBC Driver availability...")
    
    try:
        drivers = pyodbc.drivers()
        print(f"✅ Available ODBC drivers: {drivers}")
        
        sql_server_drivers = [d for d in drivers if 'SQL Server' in d]
        if sql_server_drivers:
            print(f"✅ SQL Server drivers found: {sql_server_drivers}")
            return True
        else:
            print("❌ No SQL Server drivers found")
            return False
            
    except Exception as e:
        print(f"❌ Error checking ODBC drivers: {e}")
        return False

# Usage
if __name__ == "__main__":
    driver_available = test_odbc_driver()
    if driver_available:
        print("System is ready for SQL Server connections")
    else:
        print("Please install SQL Server ODBC drivers")

Best Practices

  • Run this function during application startup or as part of health checks to ensure database connectivity prerequisites are met
  • The function uses broad exception handling which is appropriate for diagnostic purposes but may mask specific driver issues
  • Console output uses emoji which may not render correctly in all terminal environments
  • The function checks specifically for 'SQL Server' in driver names using substring matching, which may not catch all SQL Server driver variants
  • Consider calling this function before attempting to create database connections to provide better error messages to users
  • On Linux/Mac systems, ensure unixODBC is installed and configured before running this test

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function main_v41 74.6% similar

    Orchestrates and executes a suite of ODBC connectivity tests for SQL Server, providing formatted output and a summary of test results.

    From: /tf/active/vicechatdev/full_smartstat/test_odbc.py
  • function test_pyodbc_import 73.1% similar

    A diagnostic function that tests whether the pyodbc library can be imported and displays its version information and ODBC compatibility details.

    From: /tf/active/vicechatdev/full_smartstat/test_odbc.py
  • function test_connection_string 68.1% similar

    Tests the format and creation of a SQL Server connection string using SQLAlchemy with ODBC Driver 18, without establishing an actual database connection.

    From: /tf/active/vicechatdev/full_smartstat/test_odbc.py
  • function test_connection 56.5% similar

    Tests the database connection by executing a simple query and validating the result.

    From: /tf/active/vicechatdev/CDocs/db/db_operations.py
  • function test_o365_connection 52.1% similar

    Tests the connection to Microsoft Office 365 (O365) by attempting to obtain an authentication token through the O365Client.

    From: /tf/active/vicechatdev/email-forwarder/test_service.py
← Back to Browse