forked from SunJieMing/python-minicamp-homework-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh4.py
78 lines (60 loc) · 1.92 KB
/
h4.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
from flask import Flask, render_template, request, jsonify
import sqlite3
app = Flask(__name__)
@app.route('/')
def index():
return render_template('home.html')
@app.route('/movie', methods = ['POST'])
def movie():
try:
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
print('Connected to database')
#get values from the form where movie info was entered
#store in local variables
Name = request.form['name']
Language = request.form['language']
Country = request.form['country']
print(Name + ",", Language + ",", Country)
cursor.execute('INSERT INTO info VALUES (?,?,?)', (Name, Language, Country))
print('Tried inserting record')
connection.commit()
msg = 'Successfully inserted record'
print(msg)
except:
connection.rollback()
msg = 'Error while inserting record'
finally:
connection.close()
return msg
@app.route('/movies')
#retun json for all movies in the database
def movies():
try:
connection = sqlite3.connect('database.db')
cur = connection.cursor()
print('Connected to database successfully')
cur.execute('SELECT * FROM info')
result = cur.fetchall()
print('Searched the database records')
print(result)
except:
connection.rollback()
finally:
connection.close()
return jsonify(result)
@app.route('/search', methods = {'GET'})
def search():
try:
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
Name = (request.args['name']).lower()
print(Name)
cursor.execute('SELECT * FROM info WHERE LOWER(name)=?', (Name,))
srch_result = cursor.fetchall()
print(srch_result)
except:
connection.rollback()
finally:
connection.close()
return jsonify(srch_result)