-
Notifications
You must be signed in to change notification settings - Fork 0
/
apidb.py
53 lines (42 loc) · 1.56 KB
/
apidb.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
from flask import Flask, render_template, request, redirect
app = Flask(__name__, template_folder='template')
db_path = "database.db"
@app.route('/')
def home():
return render_template('home.html')
@app.route('/enter')
def get_wajah():
return render_template('wajah.html')
@app.route('/addrec', methods=['POST'])
def addrec():
try:
# Mengambil data dari formulir
id_wajah = request.form['id']
bentuk_wajah = request.form['wajah']
gambar = request.files['gambar'].read()
# Menyimpan data ke dalam database SQLite
with sqlite3.connect(db_path) as conn:
cursor = conn.cursor()
cursor.execute("INSERT INTO wajah (id_wajah, bentuk_wajah, gambar) VALUES (?, ?, ?)",
(id_wajah, bentuk_wajah, sqlite3.Binary(gambar)))
conn.commit()
print("Berhasil")
return redirect('/') # Mengarahkan pengguna kembali ke halaman utama setelah menyimpan data.
except Exception as e:
print(str(e))
print("Terjadi Kesalahan Dalam Menginput")
return redirect('/')
@app.route('/list')
def list():
try:
with sqlite3.connect(db_path) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute("SELECT * FROM wajah")
rows = cursor.fetchall()
return render_template("hasil.html", rows=rows)
except Exception as e:
print(str(e))
return "Terjadi kesalahan saat mengambil data dari database."
if __name__ == '__main__':
app.run(debug=True)