Generate text file from database

I have a event gateway script which will run every one minute and retrive data from database and write as text file. The script works fine.

Please find the script

[color=#FF0040]import os

rs = system.db.runQuery(“SELECT date_format(t_stamp, ‘%m/%d/%Y %r’), stackemission FROM pollution”)

rs1 = system.db.runQuery(“SELECT date_format(now(), ‘%Y%m%d’)”)
filename = “D:\stack_pollution\stack”+rs1[0][0]+".txt"
dir = “D:\stack_pollution”
if not os.path.exists(dir):
os.makedirs(dir)
for row in rs:
pollcontrol = ‘ngld’+’,’+‘ICLVPM’+’,’+‘1’+’,’+row[0]+’,’+‘NA’+’,’+‘NA’+’,’+‘NA’+’,’+row[1]+’\n’
system.file.writeFile(filename, pollcontrol,1)[/color]

The problem which we face is every minute it writes to the text file, the values gets added with the old value, So will have duplicates values. Please find the attached file.

Please help us to resolve the problem.
stack20110323.txt (1.09 KB)

Well, the problem is that your SQL query is returning all of the data every time. You need to add a where clause to your query that only returns the last minute or so of data.