1

I connected Python to Oracle.

I am trying to display the attributes of the table using python i.e I want to display table schema. I am using 'describe' statement but while executing it gives me an error ' Invalid SQL Statement'.

I have done the following:

queryString = 'Describe Customer'
onCursor.execute(queryString)

"Customer" is the table name

3
  • You probably connected Python to Oracle. Please describe how you did it :) Commented Nov 22, 2017 at 9:43
  • Also, could it be that you need to USE database first? Commented Nov 22, 2017 at 9:45
  • I did that but it doesn't work :( Commented Nov 22, 2017 at 9:48

3 Answers 3

6

If you need SQL statement columns description then just use cursor.description. If you need exact table schema the select from ALL_TAB_COLUMNS oracle view:

    cnn = cx_Oracle.connect(cnn_str)
    cursor = cnn.cursor()
    cursor.execute("SELECT * FROM dual")
    print(cursor.description)

    cursor.execute("select * from ALL_TAB_COLUMNS where table_name = 'DUAL'")
    print(cursor.fetchall())

    cursor.close()

It will provede output:

[('DUMMY', <class 'cx_Oracle.STRING'>, 1, 4, None, None, 1)]
[('SYS', 'DUAL', 'DUMMY', 'VARCHAR2', None, None, 1, None, None, 'Y', 1, None, None, 1, b'X', b'X', 1, 0, 1, datetime.datetime(2009, 4, 25, 23, 49, 59), 1, 'CHAR_CS', 1, 'YES', 'NO', 2, 1, 'B', 'NO', 'YES', 'NONE', 'NO', 'NO', None, None, None)]
Sign up to request clarification or add additional context in comments.

Comments

2

In below sql you replace the table and owner name. It gives the columns names and datatype of column.

SELECT COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS WHERE TABLE_NAME='table_name' and OWNER='schema name'

Comments

0

Now this is a bit of an old source, but I think your issue is that describe is not really a query.

Try getting the information from the data dictionary instead. Documentation for Oracle 12.2 is here.

1 Comment

The solution from the old source works. Thank you for the help

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.