Projet de remplacement du "RPiPasserelle" d'Otec.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db.py 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python3
  2. import sqlite3
  3. from subprocess import Popen
  4. from .conf import config
  5. import sys
  6. from pprint import pprint
  7. conn = None
  8. def connect():
  9. global conn
  10. if conn is None:
  11. print('Will connect to database {}'.format(
  12. config['heatpump']['database']))
  13. conn = sqlite3.connect(config['heatpump']['database'])
  14. conn.row_factory = sqlite3.Row
  15. return conn
  16. def initialize(filename):
  17. p = Popen(
  18. '/usr/bin/env sqlite3 -init {} {}'.format(filename, config['heatpump']['database']),
  19. shell=True
  20. )
  21. return True if p.wait() == 0 else False
  22. def sql(query):
  23. global conn
  24. if conn is None:
  25. connect()
  26. pprint(conn)
  27. cursor = conn.cursor()
  28. print(f'Will execute query : \n{query}\n')
  29. cursor.execute(query)
  30. return cursor
  31. class RowClass(object):
  32. def __init__(self, **kwargs):
  33. for key in kwargs.keys():
  34. if hasattr(self, key):
  35. setattr(self, key, kwargs[key])
  36. def select(self, key, tablename):
  37. attr = getattr(self, key)
  38. if type(attr) == str:
  39. q = f"SELECT * FROM {tablename} WHERE {key} LIKE '{attr}'"
  40. elif type(attr) == int:
  41. q = f"SELECT * FROM {tablename} WHERE {key} = {attr}"
  42. return sql(q)