application.py
import os
from flask import Flask , render_template, request
import sqlite3 as sql
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/salary", methods = ["POST"])
def salar():
name = request.form.get("employeee_name")
price = request.form.get("pay")
with sql.connect('database.db') as conn:
c=conn.cursor()
if c.execute('SELECT employee_name FROM employees WHERE employee_name=name').fetchone() is None:
c.execute('INSERT INTO employees(employee_name,basicpayment) VALUES (?,?)',(name,price))
conn.commit()
return render_template("success.html", name=name)
else:
return render_template("already.html")
if __name__ == '__main__':
app.run(debug = True)
Создание базы данных
import sqlite3
conn = sqlite3.connect('database.db')
conn.execute('CREATE TABLE employees(employee_name TEXT,basicpayment INT)')
conn.close()
index. html
<!DOCTYPE html>
<html>
</body>
<title>MY SALARY</title>
<body>
<form action="{{url_for('salar')}}" method ="POST">
<input type="text" name="employeee_name" placeholder="Enter employee name here.....">
<input type="number" name="pay" placeholder="Enter pay of employee">
<button>SUBMIT</button>
</form>
</body>
</html>
успех. html
<!DOCTYPE html>
<html>
<head>EMPLOYEE ADDED TO DATABASE</head>
<BODY>
<P>
{{name}}, has been added to database!
<a href="{{url_for('index')}}">MAKE ANOTHER ENTRY TO THE DATABASE</a>
</P>
</BODY>
</html>
уже. html
<!DOCTYPE html>
<html>
<HEAD>THIS EMPLOYEE ALREADY EXISTS IN THE DATABASE</HEAD>
<BODY>
<P>
{{name}}, already exists in the database!
<a href="{{url_for('index')}}">MAKE ANOTHER ENTRY TO THE DATABASE</a>
</P>
</BODY>
</html>
Я удалял базу данных и создавал ее заново из-за "неправильных" сообщений об ошибках. Иногда он говорит, что таблица не создана, а иногда не может найти имя столбца.