python读取Oracle

tech2023-10-12  97

#!/usr/bin/python3 import cx_Oracle,time,os,sys def get_data_from_oracle(): db = cx_Oracle.connect('user', 'pwd', '10.3.1.100:1521/xx') cursor= db.cursor() sql = "select * from order_table" print (sql + "\n") cursor.execute(sql) result = cursor.fetchall() cursor.close() db.close return result argv = sys.argv gdate = argv[1] ghour = argv[2] TMPDIR="/opt/ourpalm/scripts/tmp" retFile=TMPDIR + "/flume." + ghour + "." + str(int(time.time())) + "." + gdate + ghour + ".log" print("retFile = " + retFile + "\n") result = get_data_from_oracle() file = open(retFile, 'w') for row in result: field = "" for f in row: #print(type(f)) if None == f: field += "|" else: if isinstance(f, float): #如果是浮点类型,只保留有效数字 #'{:g}'数字超过6位默认用科学计数法表示 field += '{:.10g}'.format(f) + "|" else: field += str(f) + "|" field = field[:-1] file.write(field + "\n") file.close() os.remove(retFile) sys.exit()
最新回复(0)