Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Read a CSV and place values in MySQL Database
    primarykey
    data
    text
    <p>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. </p> <p>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. </p> <p>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.</p> <p>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?</p> <p>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.</p> <p>Hope You Guys can help..</p> <p>Thank You</p> <p>Python Code:</p> <pre><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" </code></pre> <p>My CSV file Format:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload