Connection.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import sqlite3
  2. from sqlite3 import Error
  3. import datetime as DT
  4. import numpy as np
  5. #create ConnectionToDB
  6. def create_connection(db):
  7. try:
  8. conn = sqlite3.connect(db)
  9. return conn
  10. except Error as e:
  11. print(e)
  12. return None
  13. def write_result_to_DB(category, value, conn):
  14. timestamp = DT.datetime.utcnow()
  15. cur = conn.cursor()
  16. query = "Insert into Results('Category', 'timestamp', 'Value') VALUES ('" + category + "','" + str(timestamp) + "','" + str(value) + "' )"
  17. #print(query)
  18. cur.execute(query)
  19. conn.commit()
  20. def get_result_from_db(query,target_array, conn):
  21. cur = conn.cursor()
  22. cur.execute(query)
  23. print(query)
  24. dbstring = cur.fetchone()
  25. if dbstring is None:
  26. print("NO ENTRY")
  27. else:
  28. dbstring=dbstring[0]
  29. new_list = dbstring.split(';')
  30. #print(new_list)
  31. new_arr = np.asarray(new_list)
  32. #print("arr")
  33. #print(new_arr)
  34. for entry in new_arr:
  35. value_string = entry.strip('()')
  36. value_string = str(value_string).split(', ')
  37. #print(value_string[0])
  38. #print(value_string[1])
  39. target_array = np.append(target_array, np.array([(int(value_string[0]), float(value_string[1]))], dtype=target_array.dtype))
  40. #print(result)
  41. return target_array
  42. def get_third_quartile(conn):
  43. cur = conn.cursor()
  44. t_quartile = 0
  45. query = "select value from results where category ='third_quartile' order by timestamp desc limit 1"
  46. cur.execute(query)
  47. dbstring = cur.fetchone()
  48. if dbstring is None:
  49. print("NO ENTRY")
  50. else:
  51. t_quartile = dbstring[0]
  52. return t_quartile