-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
117 lines (82 loc) · 2.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from flask import Flask, render_template, request
import sqlite3
from sqlite3 import Error
app = Flask(__name__)
@app.route("/sname", methods = ["POST"])
def search_name():
name = request.form.get("search_name")
conn = create_connection(r"products.db")
items = select_product_by_name(conn, name)
print(items)
return render_template("sname.html", items = items)
@app.route("/sprice", methods = ["POST"])
def search_price():
price = request.form.get("search_price")
conn = create_connection(r"products.db")
items = select_product_by_price(conn, price)
return render_template("sprice.html", items = items)
@app.route("/view")
def view():
conn = create_connection(r"products.db")
items= select_all_products(conn)
print(items)
return render_template("viewall.html", items = items)
@app.route("/insert", methods=["POST"])
def insert():
name = request.form.get("name")
price = request.form.get("price")
conn = create_connection(r"products.db")
insert_product(conn, (name, price))
return render_template("home.html")
@app.route("/", methods=["GET"])
def home():
table = """ CREATE TABLE IF NOT EXISTS products (
id integer PRIMARY KEY,
name text,
price integer
); """
conn = create_connection(r"products.db")
if conn is not None:
create_tables(conn, table)
else:
print("Error! cannot create the database connection.")
return render_template("home.html")
def create_connection(db_file):
conn = None
try:
conn=sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
if conn:
conn.close()
def create_tables(conn, table):
try:
c = conn.cursor()
c.execute(table)
except Error as e:
print(e)
def insert_product(conn, product):
sql = """ INSERT INTO products(name,price)
VALUES(?,?) """
cur = conn.cursor()
cur.execute(sql, product)
conn.commit()
return cur.lastrowid
def select_all_products(conn):
cur = conn.cursor()
cur.execute("SELECT * FROM products")
rows = cur.fetchall()
return rows
def select_product_by_price(conn, price):
cur = conn.cursor()
cur.execute("SELECT * FROM products WHERE price=?", (price,))
rows = cur.fetchall()
return rows
def select_product_by_name(conn, name):
cur = conn.cursor()
cur.execute("SELECT * FROM products WHERE name=?", (name,))
rows = cur.fetchall()
return rows
if __name__ == '__main__':
app.run(debug=True)