1

I need to extract a table from Access and print it in python. I have successfully connected the Access data base but I am not sure how to pull the table from Access and move it into a python data frame. I have inserted my code below.

odbc_conn_str = 'DRIVER={Microsoft Access Driver (*.mdb, 
*.accdb)};DBQ=%s;UID=%s;PWD=%s' % (db_file, user, password)
conn = pyodbc.connect(odbc_conn_str)
cur = conn.cursor()
SQLCommand = 'select *from table1'

df = cur.execute(SQLCommand)
print(df)
conn.commit()

I get no errors but all this returns is

<pyodbc.Cursor object at 0x0BCFF3A0>
1
  • try fetchall() command Commented Jun 27, 2017 at 15:59

1 Answer 1

2

The fetchall() will retrieve the result

odbc_conn_str = 'DRIVER={Microsoft Access Driver (*.mdb, 
*.accdb)};DBQ=%s;UID=%s;PWD=%s' % (db_file, user, password)
conn = pyodbc.connect(odbc_conn_str)
cur = conn.cursor()
SQLCommand = 'select * from table1'
cur.execute(SQLCommand)
df = cur.fetchall()
print(df)

You don't need to commit a select statement

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.