杰瑞科技汇

python ado access

Of course! Using Python to interact with an Microsoft Access database is a common task, especially in environments where Access is used as a front-end or for data storage. The standard and most robust way to do this is by using the pyodbc library, which is a Python DB API 2.0 module for ODBC.

Here's a comprehensive guide covering everything from setup to advanced examples.


The Core Concept: ODBC

Think of ODBC (Open Database Connectivity) as a "bridge" or a universal translator. Your Python code doesn't need to know the specific details of how to talk to an Access database. Instead, it uses the ODBC standard, and a special ODBC "driver" handles the translation for you.

The workflow is: Python Scriptpyodbc LibraryODBC Driver for Access.accdb or .mdb File


Prerequisites & Setup

Before you write any Python code, you need to set up your environment.

Step 1: Install the Python Library

You'll need pyodbc. Open your terminal or command prompt and install it using pip:

pip install pyodbc

Step 2: Install the Microsoft Access Database Engine

This is the most critical step. Python needs this engine to be able to read and write to Access files. The good news is it's free.

  1. Download the appropriate version for your system (32-bit or 64-bit).

    • Crucial: The version of the Access Database Engine you install must match the version of Python you are using.
      • If you have a 64-bit version of Python, you need the 64-bit Access Database Engine.
      • If you have a 32-bit version of Python, you need the 32-bit Access Database Engine.
    • You can check your Python version and architecture by running python -c "import struct; print(struct.calcsize('P') * 8)" in your terminal. If it prints 64, you have 64-bit Python.
  2. Download Links:

    • Microsoft Access Database Engine 2025 Redistributable (32-bit): Download Link
    • Microsoft Access Database Engine 2025 Redistributable (64-bit): Download Link
    • Note: The 2025 version is widely compatible with Access 2010, 2025, 2025, 2025, and 365 files.
  3. Run the installer. You may need to close your IDE (like VS Code or PyCharm) and reopen it after installing for the changes to be detected.


Connecting to the Access Database

The first step in your Python script is to establish a connection. The connection string is the most important part. It tells pyodbc which driver to use and where to find your file.

Connection String Template

# For .accdb files (Access 2007 and newer)
DRIVER='{Microsoft Access Driver (*.mdb, *.accdb)}'
DATABASE_PATH=r'C:\path\to\your\database.accdb'
conn_str = f'DRIVER={DRIVER};DBQ={DATABASE_PATH}'
# For .mdb files (Access 2003 and older)
# conn_str = f'DRIVER={DRIVER};DBQ=C:\\path\\to\\your\\database.mdb'

Example Connection Code

Here's how to connect, execute a simple query, and close the connection properly.

import pyodbc
# --- Connection Details ---
# Make sure the path to your database is correct
# Using a raw string (r'...') is good practice for Windows paths
db_file = r'C:\path\to\your\database.accdb'
driver = '{Microsoft Access Driver (*.mdb, *.accdb)}'
# --- Create the Connection String ---
conn_str = f'DRIVER={driver};DBQ={db_file}'
try:
    # 1. Establish a connection
    print("Connecting to the Access database...")
    conn = pyodbc.connect(conn_str)
    # 2. Create a cursor object to execute SQL queries
    cursor = conn.cursor()
    # 3. Execute a query
    # Let's assume you have a table named 'Employees'
    print("Executing query...")
    cursor.execute("SELECT * FROM Employees")
    # 4. Fetch the results
    print("Fetching results...")
    for row in cursor:
        # Each 'row' is a tuple-like object
        print(f"ID: {row[0]}, Name: {row[1]}, Department: {row[2]}")
except pyodbc.Error as e:
    print(f"Error: {e}")
finally:
    # 5. Close the connection
    if 'conn' in locals() and conn:
        print("Closing connection.")
        conn.close()

Common Database Operations (CRUD)

Here are examples for the most common database operations.

Create (INSERT)

# Assume 'conn' is your active connection object
try:
    cursor = conn.cursor()
    # Using parameters to prevent SQL injection
    # The '?' are placeholders
    sql_insert = "INSERT INTO Employees (ID, FirstName, LastName, Department) VALUES (?, ?, ?, ?)"
    employee_data = (101, 'Jane', 'Doe', 'Sales')
    cursor.execute(sql_insert, employee_data)
    # Commit the transaction to save the changes
    conn.commit()
    print("New employee added successfully.")
except pyodbc.Error as e:
    print(f"Error inserting data: {e}")
    # Rollback in case of error
    conn.rollback()

Read (SELECT)

We already saw this in the first example, but here's a more complete version with fetching methods.

try:
    cursor = conn.cursor()
    cursor.execute("SELECT ID, FirstName, Department FROM Employees WHERE Department = ?", 'Sales')
    # Method 1: Fetch one row at a time
    # row = cursor.fetchone()
    # if row:
    #     print(f"First Sales Employee: {row[1]}")
    # Method 2: Fetch all rows into a list
    all_sales_employees = cursor.fetchall()
    print("\nAll Sales Employees:")
    for emp in all_sales_employees:
        print(f"ID: {emp.ID}, Name: {emp.FirstName}")
except pyodbc.Error as e:
    print(f"Error reading data: {e}")

Update (UPDATE)

try:
    cursor = conn.cursor()
    # Update Jane Doe's department
    sql_update = "UPDATE Employees SET Department = ? WHERE FirstName = ? AND LastName = ?"
    new_dept = 'Marketing'
    first_name = 'Jane'
    last_name = 'Doe'
    cursor.execute(sql_update, new_dept, first_name, last_name)
    conn.commit()
    print(f"Updated {cursor.rowcount} employee(s).")
except pyodbc.Error as e:
    print(f"Error updating data: {e}")
    conn.rollback()

Delete (DELETE)

try:
    cursor = conn.cursor()
    # Delete the employee with ID 101
    sql_delete = "DELETE FROM Employees WHERE ID = ?"
    employee_id_to_delete = 101
    cursor.execute(sql_delete, employee_id_to_delete)
    conn.commit()
    print(f"Deleted {cursor.rowcount} employee(s).")
except pyodbc.Error as e:
    print(f"Error deleting data: {e}")
    conn.rollback()

Best Practices

  1. Always Use Parameters: Never use string formatting (f-strings or ) to insert values directly into your SQL queries. This is a major security risk called SQL Injection.

    • Bad: cursor.execute(f"SELECT * FROM Users WHERE Name = '{user_input}'")
    • Good: cursor.execute("SELECT * FROM Users WHERE Name = ?", user_input)
  2. Manage Transactions with commit() and rollback(): By default, pyodbc operates in auto-commit mode for DDL (like CREATE TABLE) but not for DML (INSERT, UPDATE, DELETE). It's best practice to explicitly manage transactions.

    • Wrap your INSERT/UPDATE/DELETE operations in a try...except block.
    • If the operation succeeds, call conn.commit().
    • If it fails, call conn.rollback() to undo any changes made during the transaction.
  3. Use Context Managers (with statement): For better resource management, you can use a context manager to ensure your connection is always closed, even if errors occur.

    import pyodbc
    db_file = r'C:\path\to\your\database.accdb'
    driver = '{Microsoft Access Driver (*.mdb, *.accdb)}'
    conn_str = f'DRIVER={driver};DBQ={db_file}'
    # The 'with' statement handles closing the connection automatically
    with pyodbc.connect(conn_str) as conn:
        with conn.cursor() as cursor:
            cursor.execute("SELECT * FROM Employees")
            for row in cursor:
                print(row)
    # Connection is automatically closed here
    print("Connection closed.")
  4. Handling Large Datasets: If you are fetching a very large table, cursor.fetchall() will load everything into memory at once, which can cause a crash. Instead, fetch rows one by one using cursor.fetchone() in a loop.

    with pyodbc.connect(conn_str) as conn:
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM HugeTable")
        while True:
            row = cursor.fetchone()
            if not row:
                break
            # Process the row here
            print(row)
分享:
扫描分享到社交APP
上一篇
下一篇