这是我为获取在特定日期之后创建的行而编写的python代码。
import csv
import os
import cx_Oracle
con = cx_Oracle.connect('{0}/{1}@{2}'.format( 'abcd', 'efgh','ijkl' ) )
cur=con.cursor()
cur.execute("select * from dbname.table_name where created_date > to_date('06-APR-16','DD-MON-YY')")
row = cur.fetchall()
fp = open('eggs.csv', 'w')
myFile = csv.writer(fp)
myFile.writerows(row)
fp.close()
cur.close()
除了我在这里提到的以外,我是否应该进口任何东西?
但我收到的错误为cx_Oracle.DatabaseError: ORA-00904: "CREATED_DATE": invalid identifier
所以有人能告诉我这里的错误是什么吗?
答案 0 :(得分:0)
您的错误似乎出现在您的SQL查询中。
根据此错误,您要查询的表中似乎没有“created_date”列:"CREATED_DATE": invalid identifier
但是,您还应该根据PEP 0008 -- Style Guide for Python Code
和常见的Python约定略微区别地编写Python。
例如:
import csv
import cx_Oracle
# connect (cleaned your formatting a bit, I assume you use real data)
con = cx_Oracle.connect('{0}/{1}@{2}'.format('abcd', 'efgh', 'ijkl'))
# execute (using the `with` context switch)
with con.cursor() as cur:
cur.execute("select * from dbname.table_name where created_date > to_date('06-APR-16','DD-MON-YY')")
row = cur.fetchall()
# write (using the `with` context switch) also csv module should be used with `rb` and `wb` modes.
with open('eggs.csv', 'wb') as fp:
csv.writer(fp).writerows(row)