parking_lot_website/database_output.py

60 lines
1.8 KiB
Python
Executable File

#!/usr/bin/python3
import pymysql
import sys
db = pymysql.connect(host='localhost', user='pi2', passwd='emb20', db='parking_lot')
cur = db.cursor()
def output_query(sql):
query = sql
cur.execute(query)
db.commit()
values = cur.fetchall()
if sql == available:
if len(values) != 0:
print("Estos son los espacios disponibles")
else:
print("Estos son los espacios ocupados")
if sql == all_values:
print("Hay {0} espacios en uso".format(len(values)))
if len(values) != 0:
print("""<br>
<table>
<tr>
<th>ID</th>
<th>Fecha</th>
<th>Hora de llegada</th>
<th>Hora de salida</th>
<th>Estado</th>
</tr>""")
for m in values:
id_s = m[0]
date= m[1]
startime= m[2]
endtime= m[3]
reserved= m[5]
if int(m[4])== 1:
estado = "Ocupado"
elif int(m[4]) == 0:
estado = "Libre"
if (int(m[5] == 1)):
estado = "Reservado"
print( "<tr><td>{0}</td> <td>{1}</td> <td>{2}</td> <td>{3}</td><td>{4}</td></tr>".format(id_s, date,startime, endtime, estado))
print("</table>")
# print(values)
# print("### Latest 10 values ###\n")
available="select * from spaces where usedID = False"
all_values="select * from spaces where usedID = True"
# last_value="select * from sensor where id_SENSOR = (select max(id_SENSOR) from sensor)"
# last_10 = "SELECT * FROM (select * from sensor order by ID_sensor DESC LIMIT 10) section order by ID_sensor ASC"
print("<h1> Espacios libres </h1>")
output_query(available)
print("<h1> Espacios en uso </h1>")
output_query(all_values)