На скорую руку
import psycopg2
from datetime import datetime, timedelta
from pprint import pprint
def get_dt(dt: str):
return datetime.strptime(dt, '%Y%m%d')
def get_dt_delta(dt: datetime, s=1, e=4):
return dt - timedelta(days=s), dt + timedelta(days=e)
def execute(config, sql, param=None, select_mode=True):
connection = None
try:
connection = psycopg2.connect(**config)
cursor = connection.cursor()
if param:
cursor.execute(sql, param)
else:
cursor.execute(sql)
if select_mode:
return cursor.fetchall()
except Exception as E:
print(E)
finally:
connection.close()
def get_devices():
devices_q = "SELECT * from devices LEFT JOIN devatt on devices.dev = devatt.dev " \
"WHERE devatt.att = 'otherinfo' AND devatt.value like '0%'"
return [x[0] for x in execute(cfg, devices_q)]
def generate_rep(devs, period):
r = {}
period_dt = get_dt(period)
period_s, period_e = get_dt_delta(period_dt)
for dev in devs:
q = 'SELECT * from rawdata where deveui = %s and time between %s AND %s ORDER BY id DESC'
rawdata = execute(config=cfg, sql=q, param=(dev, period_s, period_e))
r[dev] = rawdata
return r
if __name__ == "__main__":
cfg = dict(user="pguser", password="localpass", host="127.0.0.1", port="5432", database="wan_server")
devices = get_devices()
date = '20220308'
result = generate_rep(devs=devices, period=date)
pprint(result)
1. смотрите как правильно работать с датой и временем
2. не используйте интерполяцию в sql запросе