cx_Oracle:格式输出

时间:2018-08-13 10:17:30

标签: python oracle unix cx-oracle

您好,我正在编写一个脚本,该脚本将从Oracle数据库中获取数据。

#!/usr/bin/python

#add paths for additional libraries
import sys
sys.path.append('/app/zhunix/python/lib')
import cx_Oracle

user = '*****'
passwd = '******'
sid = '*******'
host = '**********'
port = '*******'
dsn_tns =  cx_Oracle.makedsn(host, port, sid)

db = cx_Oracle.connect(user, passwd, dsn_tns)
print("Database Version: " +  db.version)
fqdn = raw_input("Hostname: ")


var = """select USY_MAIN_FQDN,USY_SYS_ID,USY_SYS_NAME,USY_SYS_MEMORY,USY_SYS_PROCESSORS from IFC_UNIXINFRA.IFC_UNIXINFRA_SYSTEM_V where USY_SYS_NAME like ('%s')""" % fqdn

cur = db.cursor()

dbora = cur.execute(var)

for row in dbora:
        print (row)

cur.close()
db.close()

现在,当我打印“ row”时,它像这样显示(经过检查):

('servername.com', 1390863, 'servername', 147456, 8)

我如何制作看起来像这样的印刷品?

FQDN:       servername.com
ID:         1390863
Hostname:   servername
Memory:     147456
Processors: 8

在此先感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

您可以尝试如下操作:

labels = ('FQDN:', 'ID: ', 'Hostname:','Memory:','Processors:')
for row in dbora:
    for idx,el in enumerate(row): 
        print labels[idx] + ': ' + str(el)