Better ways to print out column names when using cx_Oracle

后端 未结 3 899
感动是毒
感动是毒 2021-02-01 08:39

Found an example using cx_Oracle, this example shows all the information of Cursor.description.

import cx_Oracle
from pprint import pprint

connec         


        
相关标签:
3条回答
  • 2021-02-01 08:44

    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
    
    0 讨论(0)
  • 2021-02-01 08:51

    Here the code.

    import csv
    import sys
    import cx_Oracle
    
    db = cx_Oracle.connect('user/pass@host:1521/service_name')
    SQL = "select * from dual"
    print(SQL)
    cursor = db.cursor()
    f = open("C:\dual.csv", "w")
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    r = cursor.execute(SQL)
    
    #this takes the column names
    col_names = [row[0] for row in cursor.description]
    writer.writerow(col_names)
    
    for row in cursor:
       writer.writerow(row)
    f.close()
    
    0 讨论(0)
  • 2021-02-01 08:57

    You can use list comprehension as an alternative to get the column names:

    col_names = [row[0] for row in cursor.description]

    Since cursor.description returns a list of 7-element tuples you can get the 0th element which is a column name.

    0 讨论(0)
提交回复
热议问题