In aiohttp, I am inserting data into SQLite where True or False conditions as a 1 or 0, like this as I read in the SQLite documentation that this is the best practice for Boolean:
[Date, post["starttime"], post["endtime"], 0, 1, post["setpoint"]]
For example this is in here which seems to work Ok from a junior dev standpoint:
elif weekdays:
info = f'Weekdays Found'
print(info)
response_obj = { 'status' : 'success', 'info' : info }
await db.execute(
"INSERT INTO posts (Date, starttime, endtime, Weekends, Weekdays, setpoint) VALUES(?, ?, ?, ?, ?, ?)",
[Date, post["starttime"], post["endtime"], 0, 1, post["setpoint"]],
)
await db.commit()
return web.json_response(response_obj)
When I retrieve the Data from SQLite, it does come through as a String True
or False
even though the data was inserted as a 0 or 1.
For example this is my aiohttp route to retrieve the data from the SQLite where there is most likely a much better way to write how to retrieve last row of data inserted into the db:
@routes.get('/event-payload')
async def get_event_payload(request):
ret = []
db = request.config_dict["DB"]
async with db.execute("SELECT * FROM posts ORDER BY rowid DESC LIMIT 1;") as cursor:
async for row in cursor:
ret.append(
` {
"starttime": row["starttime"],
"endtime": row["endtime"],
"Weekends": row["Weekends"],
"Weekdays": row["Weekdays"],
"setpoint": row["setpoint"],
}
)
But if I print("ret is", ret)
this will show up as True
or False
strings.
ret is [{'starttime': '13:11', 'endtime': '18:19', 'Weekends': 'True', 'Weekdays': 'True', 'setpoint': 444}]
How can I change 'Weekends': 'True', 'Weekdays': 'True'
to just Boolean operators and not just strings True
or False
?
Thanks for any tips not alot of wisdom here.
UPDATE
In aiohttp this is how I created the SQLite db
def try_make_db() -> None:
my_path = 'form_data.db'
with sqlite3.connect(my_path) as conn:
cur = conn.cursor()
print("Trying to connect to the db!")
try:
cur.execute("SELECT 1 FROM posts LIMIT 1;")
cur.close()
print("Good enough DB should be Ok")
except Exception as e:
print("Table 'posts' does not exist")
cur.execute(
"""CREATE TABLE posts (
Date TEXT PRIMARY KEY,
starttime TEXT,
endtime TEXT,
Weekends NUMBER,
Weekdays NUMBER,
setpoint NUMBER)
"""
)
print("DB TABLE CREATED")
conn.commit()