Learner Learner - 4 months ago 31
Python Question

"cannot find the input table or query" error for a SELECT statement

I am saving MS Access tables as CSV files using Python. There is a table in the MS Access database that is named 'Perm_Site Info'. There is a space in the naming in MS Access. When I run the below snippet, the code blows up. I have tried having single and as well as double quotes in the cursor.execute but no fruition. I request your kind assistance in order to understand how to fix this.

import pyodbc
import csv

conn_string = ("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Access\\permissions.accdb")

conn = pyodbc.connect(conn_string)

cursor = conn.cursor()

cursor.execute("select * from Perm_Site Info;")

with open('C:\\Desktop\\Python Files\\Perms_Site_Info.csv','wb') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([i[0] for i in cursor.description])


print 'All done for now'

The error:

cursor.execute("select * from Perm_Site Info;")
ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC Microsoft Access Driver] The Microsoft Access database engine cannot find the input table or query 'Perm_Site'. Make sure it exists and that its name is spelled correctly. (-1305) (SQLExecDirectW)")

X0r X0r

Try using brackets around the entire table name. It's barking because it doesn't know what to do with the space.

cursor.execute("select * from [Perm_Site Info];")