0

I'm trying to read the Table names from a database into a list using Pandas.read_sql. I have tried different SQL queries found online:

     cnxn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ=' + str(self.file_selected)+';Uid=Admin;Pwd=; ')
  #  sql = "SELECT * FROM SYS.TABLES" # tried this - also an error

  sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='database_name.MDB'"
    self.TableNames = pd.io.sql.read_sql(sql, cnxn)
    cnxn.close()

but I get an error that it can not find the file database_name.INFORMATION_SCHEMA.TABLES

what should I use for the sql query?

1
  • 1
    AFAIK there is no such thing as INFORMATION_SCHEMA in MS Access... Commented Mar 7, 2017 at 0:06

1 Answer 1

1

In MS Access, you can retrieve metadata on a database using its system table, MSysObjects. Below is a DML call to retrieve all table names:

SELECT MSysObjects.Name
FROM MsysObjects 
WHERE ((MSysObjects.Type)=1)
ORDER BY MSysObjects.Name;

However, by default this will not work with external ODBC calls such as you do in Python as permission is not allowed. To resolve, consider two routes:

Grant Permission (for Admin user)

Inside the MSAccess.exe GUI, open database and run VBA subroutine (in standalone module) which only needs to be run once:

Public Sub GrantMSysPermission()
   Dim strSQL As String
   strSQL = "GRANT SELECT ON MSysObjects TO Admin;"

   CurrentProject.Connection.Execute strSQL
End Sub

Once done run above query in pandas read_sql call.

Saved Table

Inside MS Access.exe GUI program, run below make-table query:

SELECT MSysObjects.Name
INTO DBTables
FROM MsysObjects 
WHERE ((MSysObjects.Type)=1)
ORDER BY MSysObjects.Name;

Then in Python pandas, refer to new table:

cnxn = pyodbc.connect('DRIVER={{Microsoft Access Driver (*.mdb)}};DBQ=' + \
                      '{};Uid=Admin;Pwd=;'.format(str(self.file_selected)))

sql = "SELECT * DBTables"
self.TableNames = pd.io.sql.read_sql(sql, cnxn)
cnxn.close()
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.