Problem polega na tym, że nie dodaje rekordów do bazy danych natomiast sczytuje już istniejące.
import os
import time
import datetime
import MySQLdb
global c
global db
def insert_to_db():
Ddate = (datetime.datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d"))
Dtime = (datetime.datetime.fromtimestamp(time.time()).strftime("%H:%M:%S"))
Dphoto = "zdjecie"
Dstring = "wykryto zagrozenie"
print (Ddate + " - " + Dtime + " - " + Dphoto + " - " + Dstring)
sql = "INSERT INTO Alarm (Ddate, Dtime, Dphoto, Dstring) VALUES (%s, %s, %s, %s)"
try:
c.execute(sql,( str(Ddate) , str(Dtime), str(Dphoto), str(Dstring)))
db.commit()
except:
db.rollback()
#db.close()
def read_from_db():
try:
#c.execute("SELECT * FROM TAB_CPU WHERE ID = (SELCET MAX(ID) FROM TAB_CPU)")
c.execute("SELECT * FROM Alarm ORDER BY ID DESC LIMIT 1")
result = c.fetchall()
if result is not None:
print ('Time: ' , result[0][1], 'Date: ' , result[0][2], ' Photo' , result[0][3], 'Info: ' , result[0][4])
except:
print ("read error")
def main():
while 1:
insert_to_db()
read_from_db()
time.sleep(10)
if __name__ == '__main__':
try:
db = MySQLdb.connect("localhost","rpi","password","SecuritySystem")
c= db.cursor()
except:
print ("Brak polaczania z serwerem...")
try:
main()
except KeyboardInterrupt:
print ("bye bye...")
pass