Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
import pyodbc

db_path = r'E:\MasterMindAcc\Sys\NetNo.accdb'
password = 'AaBbCc'

#Your connection string here...
conn_str = f'DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};DBQ={db_path};PWD={password}'

try:
    #Make the connection...
    conn = pyodbc.connect(conn_str)

    #Add your code for all your database operations here...

    #Close the connection when done...
    conn.close()

except Exception as e:
    print(f"Error: {e}")


Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

========================= RESTART: E:\MyPrj\PyDBExp.py =========================
Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

Windows 11 IoT Enterprise (64-bit)
Python 3.12.0
Microsoft Access 2007 (32-bit)

print(pyodbc.drivers())
['SQL Server']

What I have tried:

Get a record in access 2007 (.accdb) 32-bit database table where user name is admin and check user typed password is match with database record or not
Posted
Comments
Maciej Los 6-Dec-23 7:24am    
You already have asked this question: How to read access 2007 database in Python[^]
Please, do not repost!!!

1 solution

This is unfortunately what happens if you ask ChatGPT to create the code without understanding the how, when, why and what from it. There are many tutorials online that you can use to make this an easy task, if you are willing to put the effort into it - How to Connect Python to MS Access Database using Pyodbc[^]

Your code will look similar to this, ONLY as an example -
In your console, install the pyodbc -
Bash
pip install pyodbc


In Python, something like -
Python
import pyodbc

#Your Connection parameters to use your db...
db_path = r'C:\path\ToYour\database.accdb'  #Replace this with your actual db path...
driver = '{Microsoft Access Driver (*.mdb, *.accdb)}'

#Make the connection...
connection_string = f'DRIVER={driver};DBQ={db_path};'
conn = pyodbc.connect(connection_string)

#Create a cursor to test from the connection...
cursor = conn.cursor()

#Now fetch some data from your table...
table_name = 'YourTableNameHere'
cursor.execute(f'SELECT * FROM {table_name}')

#Fetch all the rows and print them to see if you were successful...
rows = cursor.fetchall()
for row in rows:
    print(row)

#Close the cursor and connection when you are done...
cursor.close()
conn.close()
 
Share this answer
 
Comments
Maciej Los 6-Dec-23 7:28am    
I've seen this question yet: How to read access 2007 database in Python[^]. And you answered it. So... Please, report this question as a repost (use red flag under the question). This is one of the rules on CP. :)
Andre Oosthuizen 6-Dec-23 13:06pm    
Done, thanks. did not realize I already answered on another post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900