Nicholas Morley
Nicholas Morley

Reputation: 4240

Get the schema of an Oracle database with python

I want to list and describe the tables present in an Oracle database.

To do this by connecting to the database with a client such as SQL Plus, a working approach is:

  1. List the tables:

    select tablespace_name, table_name from all_tables;

  2. Get columns and data types for each table:

    describe [table_name];

However when using cx_Oracle through python, cur.execute('describe [table_name]') results in an 'invalid sql' error.

How can we use describe with cx_Oracle in python?

Upvotes: 2

Views: 6038

Answers (2)

Anthony Tuininga
Anthony Tuininga

Reputation: 7086

As noted by others there is no ability to describe directly. I created a set of libraries and tools that let you do this, however. You can see them here: https://github.com/anthony-tuininga/cx_OracleTools.

Upvotes: 2

Nicholas Morley
Nicholas Morley

Reputation: 4240

It seems you can't.

From cx_Oracle instead of describe use:

cur.execute('select column_name, data_type from all_tab_columns where table_name = [table_name]')

(From Richard Moore here http://cx-oracle-users.narkive.com/suaWH9nn/cx-oracle4-3-1-describe-table-query-is-not-working)

Upvotes: 4

Related Questions