forked from parwiz123/flask-crud-application-with-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
87 lines (52 loc) · 1.92 KB
/
__init__.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
from flask import Flask, render_template, request, redirect, url_for, flash
from flask_mysqldb import MySQL
app = Flask(__name__)
app.secret_key = 'many random bytes'
app.config['MYSQL_HOST'] = '127.0.0.1'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '4115548'
app.config['MYSQL_DB'] = 'crud'
mysql = MySQL(app)
@app.route('/')
def Index():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM students")
data = cur.fetchall()
cur.close()
return render_template('index2.html', students=data )
@app.route('/insert', methods = ['POST'])
def insert():
if request.method == "POST":
flash("Data Inserted Successfully")
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
cur = mysql.connection.cursor()
cur.execute("INSERT INTO students (name, email, phone) VALUES (%s, %s, %s)", (name, email, phone))
mysql.connection.commit()
return redirect(url_for('Index'))
@app.route('/delete/<string:id_data>', methods = ['GET'])
def delete(id_data):
flash("Record Has Been Deleted Successfully")
cur = mysql.connection.cursor()
cur.execute("DELETE FROM students WHERE id=%s", (id_data,))
mysql.connection.commit()
return redirect(url_for('Index'))
@app.route('/update',methods=['POST','GET'])
def update():
if request.method == 'POST':
id_data = request.form['id']
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
cur = mysql.connection.cursor()
cur.execute("""
UPDATE students
SET name=%s, email=%s, phone=%s
WHERE id=%s
""", (name, email, phone, id_data))
flash("Data Updated Successfully")
mysql.connection.commit()
return redirect(url_for('Index'))
if __name__ == "__main__":
app.run(debug=True)