mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.95k stars 563 forks source link

i need help #1243

Closed dylzohtml555 closed 1 year ago

dylzohtml555 commented 1 year ago

i keep getting a error import pypyodbc

Replace 'C:\Users\Melmc\OneDrive - Glow Scotland\Dylz1.accdb' with the actual full path to your Dylz1.accdb file

db_file = r'C:\Users\Melmc\OneDrive - Glow Scotland\Dylz1.accdb' conn_str = f'Driver={{Microsoft Access Driver (.mdb, .accdb)}};DBQ={db_file};'

conn = None # Initialize conn outside the try block

try: conn = pypyodbc.connect(conn_str) cursor = conn.cursor()

# Create a table with an auto-incrementing ID field
cursor.execute('''
    CREATE TABLE SampleTable (
        ID INT AUTOINCREMENT PRIMARY KEY,
        Name TEXT NOT NULL,
        Age INT
    )
''')
conn.commit()

# Insert data into the table
cursor.execute("INSERT INTO SampleTable (Name, Age) VALUES (?, ?)", ("John Doe", 30))
conn.commit()

# Fetch data from the table
cursor.execute("SELECT * FROM SampleTable")
rows = cursor.fetchall()
for row in rows:
    print(row)

except Exception as e: print("An error occurred:", e)

finally:

Make sure to close the connection after use

if conn is not None:
    conn.close()