# Retrieve data from database and save it as CVS file everyday at a fixed time

**URL:** <https://forum.inductiveautomation.com/t/retrieve-data-from-database-and-save-it-as-cvs-file-everyday-at-a-fixed-time/33057>\
**Category:** Ignition\
**Tags:** ignition80, scripting, database\
**Created:** [March 13, 2020, 7:32am UTC](https://forum.inductiveautomation.com/t/retrieve-data-from-database-and-save-it-as-cvs-file-everyday-at-a-fixed-time/33057 "2020-03-13T07:32:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kairongsen](https://avatars.discourse-cdn.com/v4/letter/k/eb9ed0/32.png) [@kairongsen](https://forum.inductiveautomation.com/u/kairongsen)\
**Post date:** [March 13, 2020, 7:32am UTC](https://forum.inductiveautomation.com/t/retrieve-data-from-database-and-save-it-as-cvs-file-everyday-at-a-fixed-time/33057/1 "2020-03-13T07:32:52Z")

</div>

Greetings,

Our team is trying to retrieve data from the database and save it as a CVS file at a fixed timeslot (Let say every day1150pm) using Ignition. May I know anyone who tries to do this in Ignition through scripting? I look through the user manual and found out that the only gateway event script related to the timer is Timer Scripts but I not sure the timer scripts can be used to do this.

However, Ignition does have a function that enables us to save data in power table into CSV file  
[https://docs.inductiveautomation.com/display/DOC80/Exporting+and+Importing+a+CSV](https://docs.inductiveautomation.com/display/DOC80/Exporting+and+Importing+a+CSV)

Possible to combine these two functions to make what we wish for?

---

<div class="post-metadata">

**Author:** ![pascal](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pascal](https://forum.inductiveautomation.com/u/pascal)\
**Post date:** [March 13, 2020, 7:56am UTC](https://forum.inductiveautomation.com/t/retrieve-data-from-database-and-save-it-as-cvs-file-everyday-at-a-fixed-time/33057/2 "2020-03-13T07:56:45Z")

</div>

Hello,

If you want a script to be executed at a specific time every day, the best option for me, is to use an expression tag which should be a boolean set to True depending on your conditions. Then create a Tag Change Gateway Event Script and when your boolean becomes True, your script will execute.

For example, I created an expression tag I named trigger\_export. Here is the expression used :  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/c/cee1513d65478dc87b24d23583907a1756a6553e.png)

Then i created a Tag Change Gateway Event Script :

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/3820e136a8aac5226a6a5ae485c664d620651c60.png)

```auto
if newValue.value ==1 and initialChange==0: #If tag becomes True
	results = system.db.runQuery("SELECT * FROM example1 LIMIT 100")
	results = system.dataset.toDataSet(results)
	csv = system.dataset.toCSV(dataset = results, showHeaders = True, forExport = False)
	filePath = "C:\\output\\results.csv"
	system.file.writeFile(filePath, csv)

```

Check out this link for more info about the function to export to csv : [system.dataset.toCSV - Ignition User Manual 8.0 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC80/system.dataset.toCSV)

---

<div class="post-metadata">

**Author:** ![kairongsen](https://avatars.discourse-cdn.com/v4/letter/k/eb9ed0/32.png) [@kairongsen](https://forum.inductiveautomation.com/u/kairongsen)\
**Post date:** [March 13, 2020, 8:23am UTC](https://forum.inductiveautomation.com/t/retrieve-data-from-database-and-save-it-as-cvs-file-everyday-at-a-fixed-time/33057/3 "2020-03-13T08:23:20Z")

</div>

You are right. I can try on this. How could I never think of this method before. I will have a try on this method. Thanks for the idea. =)
