0

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:

  1. Im using same cursor object for 2 inserts. Is this OK?
  2. Do I have to close connection, cursor etc. or Python will manage this after script ends?
Kamil
  • 13,363
  • 24
  • 88
  • 183
  • 1
    See [this question](http://stackoverflow.com/q/24661754/2073595) for info on the necessity of `cursor.close()`, specifically. – dano Aug 02 '14 at 19:34

1 Answers1

0
  1. It's fine.
  2. You should close the connection.
Pankaj Sharma
  • 669
  • 5
  • 12