import pyodbc
# Connect to the database
conn = pyodbc.connect(r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\riocomponents\Advanced Data Import 3.11\demos\data\country.mdb;")
# Create a cursor
cursor = conn.cursor()
# Execute a SELECT statement to retrieve the data
cursor.execute("SELECT * FROM country")
# Fetch the rows from the cursor
rows = cursor.fetchall()
# Iterate through the rows and print the data
for row in rows:
print(row)
# Close the cursor and connection
cursor.close()
conn.close()
在此示例中,pyodbc 库用于连接到 Microsoft Access 数据库。 连接字符串指定驱动程序和数据库的位置。 然后使用游标对象执行 SELECT 语句并检索数据。 fetchall 方法用于检索 SELECT 语句返回的所有行,并使用循环打印每一行中的数据。