使用cx_Oracle时输出列名的更好方法 [英] Better ways to print out column names when using cx_Oracle
本文介绍了使用cx_Oracle时输出列名的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用cx_Oracle找到一个示例,该示例显示 Cursor.description
的所有信息。
Found an example using cx_Oracle, this example shows all the information of Cursor.description
.
import cx_Oracle
from pprint import pprint
connection = cx_Oracle.Connection("%s/%s@%s" % (dbuser, dbpasswd, oracle_sid))
cursor = cx_Oracle.Cursor(connection)
sql = "SELECT * FROM your_table"
cursor.execute(sql)
data = cursor.fetchall()
print "(name, type_code, display_size, internal_size, precision, scale, null_ok)"
pprint(cursor.description)
pprint(data)
cursor.close()
connection.close()
什么我想查看的是 Cursor.description [0]
(名称)的列表,因此我更改了代码:
What I wanted to see was the list of Cursor.description[0]
(name), so I changed the code:
import cx_Oracle
import pprint
connection = cx_Oracle.Connection("%s/%s@%s" % (dbuser, dbpasswd, oracle_sid))
cursor = cx_Oracle.Cursor(connection)
sql = "SELECT * FROM your_table"
cursor.execute(sql)
data = cursor.fetchall()
col_names = []
for i in range(0, len(cursor.description)):
col_names.append(cursor.description[i][0])
pp = pprint.PrettyPrinter(width=1024)
pp.pprint(col_names)
pp.pprint(data)
cursor.close()
connection.close()
我认为会有更好的方法打印出列名。请让我替代Python初学者。 :-)
I think there will be better ways to print out the names of columns. Please get me alternatives to the Python beginner. :-)
推荐答案
SQLAlchemy源代码是建立可靠的数据库自省方法的良好起点。这是SQLAlchemy如何从Oracle反映表名称的方法:
The SQLAlchemy source code is a good starting point for robust methods of database introspection. Here is how SQLAlchemy reflects table names from Oracle:
SELECT table_name FROM all_tables
WHERE nvl(tablespace_name, 'no tablespace') NOT IN ('SYSTEM', 'SYSAUX')
AND OWNER = :owner
AND IOT_NAME IS NULL
这篇关于使用cx_Oracle时输出列名的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文