I am trying to get values from a csv and put them into the database, I am managing to do this without a great deal of trouble.
But I know need to write back to the csv so on the next time I run the script it will only enter the values into the DB from bellow the mark in the csv file.
Note the CSV file on the system automatically flushes every 24hrs so bear in mind there might not be a mark in the csv. So basically put all values in the database if no mark is found.
I am planning to run this script every 30mins so hence there could be 48 marks in the csv file or even remove the mark and move it down the file each time?
I have been deleting the file and then re making a file in the script so new file every script run but this breaks the system somehow so that is not a great option.
Hope You Guys can help..
Thank You
Python Code:
import csv
import MySQLdb
mydb = MySQLdb.connect(host='localhost',
user='root',
passwd='******',
db='kestrel_keep')
cursor = mydb.cursor()
csv_data = csv.reader(file('data_csv.log'))
for row in csv_data:
cursor.execute('INSERT INTO `heating` VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,)',
row)
#close the connection to the database.
mydb.commit()
cursor.close()
import os
print "Done"
My CSV file Format:
2013-02-21,21:42:00,-1.0,45.8,27.6,17.3,14.1,22.3,21.1,1,1,2,2
2013-02-21,21:48:00,-1.0,45.8,27.5,17.3,13.9,22.3,20.9,1,1,2,2