Here are some options to persistently store sensor data in a database using python.
Anydbm
For simple key-value pairs the anydbm module looks the best.
It stores values as strings. Because of this you may need to use another module called pickle to convert non-string values into strings.
For example:
import anydbm, pickle from datetime import datetime #Open a database - if it doesn't exist: create it db = anydbm.open('test_data.db', 'c') time_now_in = datetime.now() string_version_in = pickle.dumps(time_now_in) #Store value db['1'] = string_version_in #Close database db.close() #Open database db2 = anydbm.open('test_data.db') string_version_out = db2['1'] time_now_out = pickle.loads(string_version_out) print time_now_out db2.close()
SQLite
For more advanced database features the next step is sqlite3.
A tutorial on sqlite3 can be found here.
You may need to install sqlite3:
sudo apt-get install sqlite3
Then in python you can use SQL statements using code such as:
import sqlite3 as lite #... - data in variables # Set up a new database connection - db will be created if it doesn't exist con = lite.connect('energymonitor.db') #with the db connection (simplifies commit etc) with con: cur = con.cursor() #Create a READINGS table if it doesn't already exist cur.execute('CREATE TABLE IF NOT EXISTS readings (r_datetime TIMESTAMP, r_watts INT, r_temp INT)') #Store sensor readings cur.execute('INSERT INTO readings VALUES(?,?,?)', (time, watts, temp))
This then stores my energy monitor readings in a database called (unoriginally) "readings":