From a50ba98e75f75c1dae33f890feceb94f351a2587 Mon Sep 17 00:00:00 2001 From: redfast00 Date: Tue, 17 Dec 2019 15:14:46 +0100 Subject: [PATCH] SQL poc --- main.py | 31 ++++++------------- templates/home.html | 74 ++++++++++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 48 deletions(-) diff --git a/main.py b/main.py index 88f7766..14bdd95 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,7 @@ -from flask import Flask, render_template, request, redirect, url_for +from flask import Flask, render_template, request, redirect, url_for, jsonify import sqlite3 -# create table orders (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, remark TEXT, created_at dat -#etime default current_timestamp, status text default "besteld"); +# create table orders (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, remark TEXT, created_at datetime default current_timestamp, status text default "besteld"); # c = conn.cursor() # Create table @@ -18,9 +17,13 @@ def execute_query(query): conn = sqlite3.connect('user.db') c = conn.cursor() + print(f"{query=}") + results = [] + # Insert a row of data try: c.execute(query) + results = c.fetchall() except: print("An sql execute error occured") @@ -30,38 +33,24 @@ def execute_query(query): # We can also close the connection if we are done with it. # Just be sure any changes have been committed or they will be lost. conn.close() + return results -def fetch_orders(): - conn = sqlite3.connect('user.db') - c = conn.cursor() - - c.execute( - "select name, remark, status, id from orders order by created_at") - rows = c.fetchall() - - return [{ - "name": row[0], - "remark": row[1], - "status": row[2], - "id": row[3] - } for row in rows] - pancake_statuses = ["besteld", "aan_het_bakken", "klaar"] @app.route("/") def home(): - return render_template("home.html", orders=fetch_orders()) + return render_template("home.html") @app.route("/execute", methods=["POST"]) def execute(): content = request.json print("Query : ", content) - execute_query(content["lecode"]) - return redirect("/") + response = execute_query(content["lecode"]) + return jsonify(response) if __name__ == "__main__": diff --git a/templates/home.html b/templates/home.html index 029175c..d0e8944 100644 --- a/templates/home.html +++ b/templates/home.html @@ -17,44 +17,64 @@

Zie hieronder de lijst van personen die een pannenkoek willen

- - {% for order in orders %} - - - - - - - - {% endfor %} +
{{ order.name + order.remark + ": " }}{{ "Huidige status: " + order.status }}