28

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.

4
  • query ODBC Schema ? Commented Aug 27, 2014 at 9:03
  • Sorry for my ignorance, but how is that applied in python odbc? Commented Aug 27, 2014 at 9:17
  • Which Python ODBC library are you using? Commented Aug 27, 2014 at 9:28
  • ODBC (and not pyODBC) Commented Aug 27, 2014 at 9:49

1 Answer 1

40

(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...

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

3 Comments

Thx. Though, I am using the standard anaconda package, and I can only use odbc and not pyodbc (and I can't just install packages due to security) - the cursor in odbc does not have tables() attribute. Is there any solution to this?
@user2866103 Edited for PyWin32-odbc
This is the best answer after hours of searching SO!

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.