parse a csv file and insert or update SQL database table
Posted: Tue Dec 16, 2014 11:15 am
I have receive a csv file (ftp download every 30 minutes) that contains temperature reading at 15 minute intervals (the first 2 lines and the last lines are blank). The reading are for the last 7 days but I only need to load any readings I don't already have, in to the database table. The reason for 7 days is if there are any issues at my end I have 7 days to resolve them before I start missing data. So the file that is about 680 lines long I normally will only need the last 2 temperature readings. Details on the csv and database are below.
I know how to retrieve the file every 30 minutes. But don't know how to parse the file and select only the required records and insert them into the table.
My thoughts are to retrieve that last record in the database table to a variable and then start parsing the csv file until I get to that record and then do only action the lines after it (now sure if this is the best way). What other options if any and could you write a task to demonstrate how it would work.
CSV file: acacia_bay_temperature.csv
The first field: "ACACIA_WT" needs to be discarded as I don't need it.
The second field: is the date of the reading in yyyymmdd format and needs to be combined with the time field (below) and converted to a datetime for loading in the database.
The third field: is the time of the reading in HHMM format and needs to be combined with the date field (above) and converted to a datetime for loading in the database.
The forth field: is the temperature and needs to be stored as a decimal in the database.
Database Table: temperatures
First field: TempDateTime
Second Field: TempReading
csv file sample
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++not part of file just to show the beginning
ACACIA_WT,20141209,1435,15.9
ACACIA_WT,20141209,1450,15.7
ACACIA_WT,20141209,1505,15.7
ACACIA_WT,20141209,1520,15.7
ACACIA_WT,20141209,1535,16.3
ACACIA_WT,20141209,1550,15.8
ACACIA_WT,20141209,1605,16.2
ACACIA_WT,20141209,1620,15.9
ACACIA_WT,20141209,1635,15.8
ACACIA_WT,20141209,1650,16.0
ACACIA_WT,20141209,1705,16.0
ACACIA_WT,20141209,1720,16.1
ACACIA_WT,20141209,1735,16.3
ACACIA_WT,20141209,1750,16.5
ACACIA_WT,20141209,1805,16.5
ACACIA_WT,20141209,1820,16.5
ACACIA_WT,20141209,1835,16.5
ACACIA_WT,20141209,1850,16.5
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++not part of file just to show the end
I know how to retrieve the file every 30 minutes. But don't know how to parse the file and select only the required records and insert them into the table.
My thoughts are to retrieve that last record in the database table to a variable and then start parsing the csv file until I get to that record and then do only action the lines after it (now sure if this is the best way). What other options if any and could you write a task to demonstrate how it would work.
CSV file: acacia_bay_temperature.csv
The first field: "ACACIA_WT" needs to be discarded as I don't need it.
The second field: is the date of the reading in yyyymmdd format and needs to be combined with the time field (below) and converted to a datetime for loading in the database.
The third field: is the time of the reading in HHMM format and needs to be combined with the date field (above) and converted to a datetime for loading in the database.
The forth field: is the temperature and needs to be stored as a decimal in the database.
Database Table: temperatures
First field: TempDateTime
Second Field: TempReading
csv file sample
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++not part of file just to show the beginning
ACACIA_WT,20141209,1435,15.9
ACACIA_WT,20141209,1450,15.7
ACACIA_WT,20141209,1505,15.7
ACACIA_WT,20141209,1520,15.7
ACACIA_WT,20141209,1535,16.3
ACACIA_WT,20141209,1550,15.8
ACACIA_WT,20141209,1605,16.2
ACACIA_WT,20141209,1620,15.9
ACACIA_WT,20141209,1635,15.8
ACACIA_WT,20141209,1650,16.0
ACACIA_WT,20141209,1705,16.0
ACACIA_WT,20141209,1720,16.1
ACACIA_WT,20141209,1735,16.3
ACACIA_WT,20141209,1750,16.5
ACACIA_WT,20141209,1805,16.5
ACACIA_WT,20141209,1820,16.5
ACACIA_WT,20141209,1835,16.5
ACACIA_WT,20141209,1850,16.5
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++not part of file just to show the end