如何使用 python 在 oracle 中显示 table 的属性?
How do I display attributes of table in oracle using python?
我已将 Python 连接到 Oracle。
我正在尝试使用 python 显示 table 的属性,即
我想显示 table 架构。我正在使用 'describe' 语句,但在执行时出现错误 ' Invalid SQL Statement'.
我做了以下事情:
queryString = 'Describe Customer'
onCursor.execute(queryString)
"Customer" 是 table 名字
现在 this 有点过时了,但我认为你的问题是 describe
并不是真正的查询。
尝试从数据字典中获取信息。 Oracle 12.2 的文档是 here.
如果您需要 SQL 语句列描述,则只需使用 cursor.description。如果您需要 table 模式 ALL_TAB_COLUMNS oracle 视图中的 select:
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()
它将证明输出:
[('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)]
在下面 sql 中替换 table 和所有者名称。它给出列的列名和数据类型。
SELECT COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS WHERE TABLE_NAME='table_name' and OWNER='schema name'
我已将 Python 连接到 Oracle。
我正在尝试使用 python 显示 table 的属性,即
我想显示 table 架构。我正在使用 'describe' 语句,但在执行时出现错误 ' Invalid SQL Statement'.
我做了以下事情:
queryString = 'Describe Customer'
onCursor.execute(queryString)
"Customer" 是 table 名字
现在 this 有点过时了,但我认为你的问题是 describe
并不是真正的查询。
尝试从数据字典中获取信息。 Oracle 12.2 的文档是 here.
如果您需要 SQL 语句列描述,则只需使用 cursor.description。如果您需要 table 模式 ALL_TAB_COLUMNS oracle 视图中的 select:
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()
它将证明输出:
[('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)]
在下面 sql 中替换 table 和所有者名称。它给出列的列名和数据类型。
SELECT COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS WHERE TABLE_NAME='table_name' and OWNER='schema name'