- 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.
- To create a database employee.DB and the table Employees in SQLite using the following python script.
- 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.
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.
- 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.
- 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 HTML template delete.html is given below.
- 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.
- 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.
The full python script is given below.
- Run the python script EmployeeDB.py to create the database and the Employees table using the following command on the terminal.
$ python EmployeeDB.py
- Click on the link Add Employee to add a new employee to the database.
- Fill this form and click submit to save the details into the database
- Now click on the view employee to list all the employees of the database.
SQLite Employee Output
- Now click on the Delete Record
- Enter any employee id for which the records are to be deleted
- Hence, the record for the employee with id 1 is deleted. Here, we can confirm this by viewing the list. Click View List to view the list.
If you want to learn about Python Course , you can refer the following links Python Training in Chennai , Machine Learning Training in Chennai , Data Science Training in Chennai , Artificial Intelligence Training in Chennai