mkleehammer/pyodbc

i need help

dylzohtml555 opened this issue · 0 comments

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()