# Importing a CSV file into a dataset, filter it by date, then into a postgresql database

**URL:** <https://forum.inductiveautomation.com/t/importing-a-csv-file-into-a-dataset-filter-it-by-date-then-into-a-postgresql-database/17643>\
**Category:** Ignition\
**Tags:** scripting, database, ignition79\
**Created:** [April 9, 2018, 5:16pm UTC](https://forum.inductiveautomation.com/t/importing-a-csv-file-into-a-dataset-filter-it-by-date-then-into-a-postgresql-database/17643 "2018-04-09T17:16:19Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![ethomason](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ethomason/32/43338_2.png) [@ethomason](https://forum.inductiveautomation.com/u/ethomason)\
**Post date:** [April 9, 2018, 8:38pm UTC](https://forum.inductiveautomation.com/t/importing-a-csv-file-into-a-dataset-filter-it-by-date-then-into-a-postgresql-database/17643/5 "2018-04-09T20:38:00Z")

</div>

Actually this was intriguing so I poked around and will share my results. I created a csv file with the following data:

```auto
date,pressure,temp
2018-01-01 12:00:00,10,70
2018-01-08 12:00:00,12,82
2018-01-15 12:00:00,8,75
2018-01-22 12:00:00,15,80.66666667
2018-01-29 12:00:00,45,83.16666667
2018-02-03 12:00:00,16,85.66666667
2018-02-10 12:00:00,34,88.16666667
2018-02-14 12:00:00,36.71428571,90.66666667
2018-02-20 12:00:00,40.89285714,93.16666667
2018-03-01 12:00:00,45.07142857,95.66666667
2018-03-14 12:00:00,49.25,98.16666667
2018-03-21 12:00:00,53.42857143,100.6666667
2018-04-01 12:00:00,57.60714286,103.1666667
2018-04-04 12:00:00,61.78571429,105.6666667
2018-04-09 12:00:00,65.96428571,108.1666667

```

Then I dropped a Date Range Component onto a window and added a button with the following code:

```auto
import csv
 
startDate = event.source.parent.getComponent('Date Range').startDate
endDate = event.source.parent.getComponent('Date Range').endDate

path = "C:/projects/misc/example.csv"
 
reader = csv.DictReader(open(path))
for row in reader:
	date = system.date.parse(row["date"], 'yyyy-MM-dd')
	if system.date.isBetween(date, startDate, endDate):
		print row
		system.db.runPrepUpdate("INSERT INTO csv_data (date, pressure, temp) VALUES (?,?,?)", [date, row["pressure"], row["temp"]], "test")

```

I believe this accomplishes all of your objectives, right?

---

_[View the full topic](https://forum.inductiveautomation.com/t/importing-a-csv-file-into-a-dataset-filter-it-by-date-then-into-a-postgresql-database/17643)._
