SQLite3 module of the python to create the database web applications. Here we will Create a CRUD (create - read - update - delete) application.
CRUD Application in Flask
Here we will manage the employee information in the SQLite database.
Sample code
To create a database employee.DB and the table Employees in SQLite using the following python script.
EmoloyeeDB.py
Let us look at the view function: index() which is associated with the URL (/). It renders a template index.html.
The following HTML template (index.html) is considered as the home page of our application. It provides the links using which we can add, view, and delete the data stored in the database.
index.html
The view function add() which is associated with the URL (/add) renders the template add.html given below. It provides the form to enter the employee information.
add.html
All the details entered by the Admin is posted to the URL/savedetails which is associated with the function saveDetails(). It also generates the message depending upon the cases in which the data is successfully inserted, or some error occurred.
It renders a template success.html to display the message to the admin. It also contains a link to view the records entered by the user.
success.html
The delete() function is associated to the URL /delete. It renders an HTML template delete.html which provides the form to the admin that prompts to enter the Employee_Id of which the records are to be deleted. It also contains a link to the /view URL that shows all the records to the admin.
The Employee_Id entered by the admin is posted to the URL /deleterecord which contains the python code to establish the connection to the database and then delete all the records for the specified Employee ID. The URL /deleterecord is associated with the function deleterecord() which is given below.
The function deleterecord() generates a message depending upon the scenario whether the data is successfully deleted or some error occurred. It renders an HTML template delete_record.html to display the message to the admin.
delete_record.html
The template delete_record.html contains a link to the URL /view which shows the Employee records to the admin.
The HTML template view.html which shows all the information on the browser is given below.
view.html
The full python script is given below.
crud.py
Run the python script EmployeeDB.py to create the database and the Employees table using the following command on the terminal.