# Generate text file from database

**URL:** <https://forum.inductiveautomation.com/t/generate-text-file-from-database/2797>\
**Category:** Ignition\
**Created:** [March 23, 2011, 3:15pm UTC](https://forum.inductiveautomation.com/t/generate-text-file-from-database/2797 "2011-03-23T15:15:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@alex](https://forum.inductiveautomation.com/u/alex)\
**Post date:** [March 23, 2011, 3:15pm UTC](https://forum.inductiveautomation.com/t/generate-text-file-from-database/2797/1 "2011-03-23T15:15:19Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/1X/738ea1c799d8cec258633d4895b135f68b5597ac.txt) (1.09 KB)

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [March 23, 2011, 5:55pm UTC](https://forum.inductiveautomation.com/t/generate-text-file-from-database/2797/2 "2011-03-23T17:55:35Z")

</div>

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.
