【发布时间】:2020-06-18 15:16:03
【问题描述】:
以下代码用于从 csv 文件中的 oracle 数据库中提取数据。
在查询中,为了将小数转换为日期格式,我使用了To_Date('12/30/1899', 'MM/DD/YYYY HH24:MI:SS')+DTIMESTAMP) Decoded_Date。
并且还指定了提取日期之间数据的日期范围。 请帮助下面给出无效语法的代码有什么问题。
import csv
import cx_Oracle
dsn_tns = cx_Oracle.makedsn('hostname', 'port', sid='sid') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'username', password='password', dsn=dsn_tns)
cursor = conn.cursor()
csv_file = open("C:/Users/locations.csv", "w")
writer = csv.writer(csv_file, delimiter=',', lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
r = cursor.execute("""SELECT *
FROM (SELECT LROWNUM,DTIMESTAMP,LSCENARIO,LYEAR,LPERIOD,
LENTITY,LPARENT,LVALUE,LACCOUNT,LICP,LCUSTOM1,
LCUSTOM2,STRUSERNAME,STRSERVERNAME,
LACTIVITY,DDATAVALUE,BNODATA,
(To_Date('12/30/1899', 'MM/DD/YYYY HH24:MI:SS')+DTIMESTAMP) Decoded_Date
FROM TABLE_NAME
) SUB
WHERE SUB.Decoded_Date between '23-MAR-2020' and '24-APR-2020';
""")
for row in cursor:
writer.writerow(row)
cursor.close()
conn.close()
csv_file.close()
【问题讨论】: