Im Python newbie.
I wrote simple script which runs every minute on my Raspberry Pi and it logs some data to MySQL 5.5 database.
Script looks like this:
#!/usr/bin/python
import MySQLdb as mdb
from MyADCPi import ADCPi
import time
import os
con = mdb.connect('localhost','pi','pi','pi')
#initialize MCP3424 ADC in 18 bit mode
adc = ADCPi(18)
# initialize cursor
cur = con.cursor()
# measure LM35 output voltage and calculate temperature
# 1'C = 10mV, 1V = 100'C
temp = adc.readVoltage(1)*100
# log temperature
cur.execute("INSERT INTO data(channel, value) values(1,"+str(temp)+")")
# measure MPX4115 voltage and calculate pressure
# (19.5/7.5) is voltage divider
# vs is sensor supply voltage
# (vo/vs + 0.095)/0.0009 comes from formula in MPX4115 datasheet
vo = adc.readVoltage(2)*(19.5/7.5)
vs = 4.8
press = (vo/vs + 0.095)/0.0009
# log pressure
cur.execute("INSERT INTO data(channel, value) values(2,"+str(press)+")")
con.commit()
Questions:
- Im using same cursor object for 2 inserts. Is this OK?
- Do I have to close connection, cursor etc. or Python will manage this after script ends?