[英]Get the schema of an Oracle database with python
I want to list and describe the tables present in an Oracle database. 我想列出并描述Oracle数据库中存在的表。
To do this by connecting to the database with a client such as SQL Plus, a working approach is: 通过使用诸如SQL Plus之类的客户端连接到数据库来实现此目的,一种可行的方法是:
List the tables: 列出表:
select tablespace_name, table_name from all_tables;
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. 但是,当通过python使用cx_Oracle时,
cur.execute('describe [table_name]')
导致“无效的sql”错误。
How can we use describe
with cx_Oracle in python? 我们如何在Python中的cx_Oracle中使用
describe
?
It seems you can't. 看来你做不到。
From cx_Oracle instead of describe
use: 从cx_Oracle而不是
describe
使用:
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 ) (从Richard Moore此处http://cx-oracle-users.narkive.com/suaWH9nn/cx-oracle4-3-1-describe-table-query-is-not-working )
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 .
您可以在这里看到它们: https : //github.com/anthony-tuininga/cx_OracleTools 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.