-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
44 lines (35 loc) · 1.09 KB
/
app.py
File metadata and controls
44 lines (35 loc) · 1.09 KB
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
"""
@Project: LiveSearchInDatabaseWeb
@Author: loyio
@Date: 6/10/21
"""
from flask import Flask, render_template, request, jsonify
import pymysql
app = Flask(__name__)
# Enter here your database informations
conn = pymysql.connect(host='localhost',
user='root',
password='123456',
database='python_quiz_db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/livesearch", methods=["POST", "GET"])
def livesearch():
searchtext = request.form.get("text")
cursor = conn.cursor()
sql = "select * from MultipleChoiceQuestion where QQuestion LIKE '%"+searchtext+"%' order by QNo"
try:
res = [int(cursor.execute(sql))]
if res[0] == 0:
res.append("error")
else:
res.append(cursor.fetchall())
except Exception as e:
res = [0, "error"]
print(res[1])
return jsonify(res[1])
if __name__ == "__main__":
app.run(debug=True)