Better ways to print out column names when using cx_Oracle

后端 未结 3 903
感动是毒
感动是毒 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: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()
    

提交回复
热议问题