我有一个代码可以将查询结果保存到csv文件中。是否可以根据查询结果制作条件?例如:
如果查询结果包含字符串“Test”,则执行另一个查询并将其保存到现有文件。
我应该使用“fetchone”方法吗?
我的代码:
try:
connection = cx_Oracle.connect('TAGATE','TAGATE','GPSPL')
cursor = connection.cursor()
print "connected"
try:
query = """select example1 , example 2 from dbo1""".format(line_name)
tmp = cursor.execute(query)
columns = [i[0] for i in cursor.description]
results = tmp.fetchall()
cursor.close()
except:
pass
except:
print IOError
filename='{0}.csv'.format(line_name)
if results:
csv_file = open(filename,'wb')
myFile = csv.writer(csv_file)
myFile.writerow(columns)
myFile.writerows(results)
#i think another statment should be here
答案 0 :(得分:0)
我会做以下事情:
...
if results:
for ex1, ex2 in results:
# I assumed string was to be found in field 'Example1'
if 'Test' in ex1:
# Save your results as you do with csv_file = open(filename,'wb') and so on