SoFunction
Updated on 2025-04-05

Summary of two ways to connect to access database in Python

Preface

Record two ways of access in python

1. SQLalchemy connection access

sqlalchemy-access dialect version connects to the access database

1.Introduce the library

The code is as follows (example):

pip install sqlalchemy-access

2. Connect

The code is as follows (example):

import urllib
from sqlalchemy import create_engine
connection_string = (
        r"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};"
        f"DBQ={db_path};"
        r"ExtendedAnsiSQL=1;"
    )
connection_uri = f"access+pyodbc:///?odbc_connect={.quote_plus(connection_string)}"
engine = create_engine(connection_uri)
query = """
             SELECT *
             FROM table name
             WHERE field LIKE 'info';
             """
with () as connection:#Read the database	df_result = pd.read_sql_query(query,engine)

with new_engine.connect() as con:#Write to a new database	df_result.to_sql(table_name, con, if_exists='replace', index=False)

2. pyodbc connection access

Use pyodbc module to connect

1.Introduce the library

The code is as follows (example):

pip install pyodbc

2. Connect

The code is as follows (example):

import pyodbc
import pandas as pd

# Database file pathdb_file_path = r'your/mdb/file/'
# Create a connection using pyodbcconn = (rf'Driver={{Microsoft Access Driver (*.mdb, *.accdb)}};DBQ={db_file_path};')
query = """
             SELECT *
             FROM table name
             WHERE field LIKE 'info';
             """
# Read with pandasdf = pd.read_sql_query(query, conn)

3. Access connection string (reference)

# ODBC Standard Security PolicyDriver={Microsoft Access Driver (*.mdb)};Dbq=C:\;Uid=Admin;Pwd=;

# Working GroupDriver={Microsoft Access Driver (*.mdb)};Dbq=C:\;SystemDB=C:\;

# Exclusive modeDriver={Microsoft Access Driver (*.mdb)};Dbq=C:\;Exclusive=1;Uid=admin;Pwd=;

# Administrator Mode If you need to use commands such as CREATE USER, CREATE GROUP, ADD USER, GRANT, REVOKE and DEFAULTS in your program, you need to use this connection string.Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\;Uid=Admin;Pwd=;ExtendedAnsiSQL=1;

# OLE DB, OleDbConnection (.NET) Standard Security PolicyProvider=.4.0;Data Source=C:\;User Id=admin;Password=;

//Use the database password If your Access database has a password set, you need to refer to the following connection string to write the password before it can be used normally.Provider=.4.0;Data Source=C:\;Jet OLEDB:Database Password=MyDbPassword;

# Working Group (System Database)Provider=.4.0;Data Source=C:\;Jet OLEDB:System Database=;

# Working group (system database) and develop username and passwordProvider=.4.0;Data Source=C:\;Jet OLEDB:System Database=;User ID=myUsername;Password=myPassword;

Attachment: [Database connection string] Access connection string

//ODBC Standard Security PolicyDriver={Microsoft Access Driver (*.mdb)};Dbq=C:\;Uid=Admin;Pwd=;

//Work GroupDriver={Microsoft Access Driver (*.mdb)};Dbq=C:\;SystemDB=C:\;

//Exclusive modeDriver={Microsoft Access Driver (*.mdb)};Dbq=C:\;Exclusive=1;Uid=admin;Pwd=;

//Administrator mode If you need to use commands such as CREATE USER, CREATE GROUP, ADD USER, GRANT, REVOKE and DEFAULTS in your program, you need to use this connection string.Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\;Uid=Admin;Pwd=;ExtendedAnsiSQL=1;

//OLE DB, OleDbConnection (.NET) Standard security policyProvider=.4.0;Data Source=C:\;User Id=admin;Password=;

//Use the database password If your Access database has a password set, you need to refer to the following connection string to write the password before it can be used normally.Provider=.4.0;Data Source=C:\;Jet OLEDB:Database Password=MyDbPassword;

//Work Group (system database)Provider=.4.0;Data Source=C:\;Jet OLEDB:System Database=;

//Work Group (system database) and formulate username and passwordProvider=.4.0;Data Source=C:\mydatabase

Personally recommend pyodbc, simple.

Tip: Here we record the pits experienced for easy access.

This is the article about the two ways to connect to the access database of Python. For more related contents of Python connection to the access database, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!