Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python odbc; how to find all tables in an odbc

Tags:

python

odbc

Is there any way to get a list of all tables available through an odbc connection.

I have to fetch data from tables that are generated along the way, and therefore I don't know the names in advance.

like image 284
user2866103 Avatar asked Aug 27 '14 08:08

user2866103


1 Answers

(the ODBC driver was not specifie at time of this answer)
From PyODBC documentation:

Most of the ODBC catalog functions are available as methods on Cursor objects. The results are presented as SELECT results in rows that are fetched normally. The Cursor page documents these, but it may be helpful to refer to Microsoft's ODBC documentation for more details.

cnxn   = pyodbc.connect(...)
cursor = cnxn.cursor()
for row in cursor.tables():
    print row.table_name

EDIT: as OP specified using "Anaconda ODBC":

As far as I know, there is no direct access to those data from PyWin32-odbc (as I think this is what Anacondas uses). Depending your underlying DB they might be a "system table" that you might query. Something like sys.objects or dbo.sysobjects or information_schema.tables or ... (RDBMS vendors are very creative in that area).

Please see the doc of the underlying RDBMS for more info. Or (as of myself, I would strongly push toward that), file a request to install a more versatile ODBC driver...

like image 151
Sylvain Leroux Avatar answered Sep 21 '22 18:09

Sylvain Leroux