conn, cursor = self.__connect()
try:
cursor.execute("CREATE TABLE cm160data(ts timestamp, current float, UNIQUE (ts))")
conn, cursor = self.__connect()
try:
cursor.execute("CREATE TABLE cm160data(ts timestamp, current float, UNIQUE (ts))")
- # Attempt to create table in case this is first time to run
- # the server; but thereafter we expect an exception because the
- # create will fail...
+ # Attempt to create table in case this is the first time we are launched.
+ # But thereafter we expect an exception because the create will fail if the table already exist
self.info("MySQL database OK: %s" % (self.__db) )
def store(self, cm160Data):
self.info("MySQL database OK: %s" % (self.__db) )
def store(self, cm160Data):
"""Store the CM160 data in the MySQL database.
There may be duplicate timestamps in the CM160data.
If the data was added to the database return True, if
not added to the database return False"""
"""Store the CM160 data in the MySQL database.
There may be duplicate timestamps in the CM160data.
If the data was added to the database return True, if
not added to the database return False"""
- #Create a connection if we don't already have one
- if self.__StoreConn == None:
- self.__StoreConn, self.__StoreCursor = self.__connect()
+ #Create a connection
+ StoreConn, StoreCursor = self.__connect()
- self.__StoreCursor.execute("""INSERT INTO cm160data VALUES (%s,%s)""", (cm160Data.getDatetime(),cm160Data.getAmps(),))
+ StoreCursor.execute("""INSERT INTO cm160data VALUES (%s,%s)""", (cm160Data.getDatetime(),cm160Data.getAmps(),))
+ StoreConn.commit()
+ StoreConn.close()
+
# INSERT will fail this way if this is a duplicate
# record (as it commonly can be when receiving 30-day
# record from CM160 device!)
# INSERT will fail this way if this is a duplicate
# record (as it commonly can be when receiving 30-day
# record from CM160 device!)
if self.__displayTime < time.time():
self.info("stored record: %s" % cm160Data)
self.__displayTime=time.time()+2
if self.__displayTime < time.time():
self.info("stored record: %s" % cm160Data)
self.__displayTime=time.time()+2