Flask and Sqlite Basic Crud Python Tutorial
Flask app.py
from flask import Flask, render_template, request, redirect
import sqlite3
app = Flask(__name__)
@app.route('/')
def view():
con = sqlite3.connect("product.db")
con.row_factory =sqlite3.Row
cur = con.cursor()
cur.execute("SELECT * FROM PRODUCTS")
rows = cur.fetchall()
return render_template("viewdata.html",rows=rows)
@app.route('/add<int:id>')
def add(id):
con= sqlite3.connect("product.db")
con.row_factory =sqlite3.Row
cur = con.cursor()
cur.execute("INSERT INTO KART2 (id,name,price) SELECT id,name,price FROM PRODUCTS WHERE id = ?",[id])
con.commit()
cur = con.cursor()
cur.execute("SELECT * FROM kart2")
rows = cur.fetchall()
return render_template("kart.html",rows=rows)
@app.route('/kart')
def kart():
con = sqlite3.connect("product.db")
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("SELECT * FROM kart2")
rows = cur.fetchall()
return render_template("kart.html", rows=rows)
@app.route('/update/<int:id>',methods = ['POST','GET'])
def update(id):
if request.method == 'POST':
quantity = request.form['quantity']
con = sqlite3.connect("product.db")
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("UPDATE KART2 SET quantity=? WHERE id = ?",[quantity,id])
con.commit()
return redirect('/kart')
@app.route('/delete/<int:id>')
def delete(id):
con = sqlite3.connect("product.db")
con.row_factory=sqlite3.Row
cur = con.cursor()
cur.execute("DELETE FROM kart2 WHERE id = ?",[id])
con.commit()
rows = cur.fetchone()
return redirect('/kart')
@app.route('/price')
def price():
con = sqlite3.connect("product.db")
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("SELECT * FROM KART2")
rows = cur.fetchall()
sum =0
for row in rows:
total = row[2]*row[3]
sum += total
return render_template("orders.html",rows=rows,sum=sum)
app.run()
view data.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>view data</title>
<style>
table{
font-family:ariel,sans-serif;
border-collapse:collapse;
width:20%;
}
td{
border:5px solid #dddddd;
text-align:left;
padding:8px;
}
</style>
</head>
<body>
<table>
<thead>
<td>id</td>
<td>Name</td>
<td>price</td>
</thead>
{% for row in rows %}
<tr>
<td>{{row["id"]}}</td>
<td>{{row["name"]}}</td>
<td>{{row["price"]}}</td>
<td><a href="/add{{row.id}}">add product</a></td>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
kart.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>kart</title>
<style>
table{
font-family:ariel,sans-serif;
border-collapse:collapse;
width:20%;
}
td{
border:5px solid #dddddd;
text-align:left;
padding:8px;
}
</style>
</head>
<body>
<table>
<thead>
<td>id</td>
<td>Name</td>
<td>price</td>
<td>quanity</td>
</thead>
{% for row in rows %}
<tr>
<td>{{row["id"]}}</td>
<td>{{row["name"]}}</td>
<td>{{row["price"]}}</td>
<td>{{row["quantity"]}}</td>
<form method="POST" action='{{url_for("update",id = row.id)}}'>
<td> <input type = "number" name ="quantity" min="1" max="5" value="{{row.quantity}}"></td>
<td><input type = "submit"></td>
</form>
<td><a href ="/delete/{{row.id}}"> Delete</a></td>
</tr>
{% endfor %}
</table>
<a href ="/price">orders</a><br>
<a href ="/">Add Products</a>
</body>
</html>
Orders.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>orders</title>
<style>
table{
font-family:ariel,sans-serif;
border-collapse:collapse;
width:20%;
}
td{
border:5px solid #dddddd;
text-align:left;
padding:8px;
}
</style>
</head>
<body>
<table>
<thead>
<td>id</td>
<td>Name</td>
<td>price</td>
<td>quanity</td>
</thead>
{% for row in rows %}
<tr>
<td>{{row["id"]}}</td>
<td>{{row["name"]}}</td>
<td>{{row["price"]}}</td>
<td>{{row["quantity"]}}</td>
<td> <a href ="/kart">edit</a></td>
</tr>
{% endfor %}
</table>
<a href ="/">Home</a>
<p>Total price{{sum}}</p>
</body>
</html>
Output
Comments
Post a Comment