Flask SQLite

Flask allows us to use the SQLite3 which is one of the python modules. It is used for creating database web applications. To understand this, we will be going to create a CRUD application.

CRUD Application in the Flask

 Here we will manage the student information in the SQLite using the flask script, and admin can interact with it. This database named student.db contains related tables, and these tables contain information about the students.

  1. Create a database named student.db and table  “student” in the SQLite using the python script given below.

StudentDb.py

  •  In order to build a crud application in the Flask, we have to focus on the view functions like index()  and controllers to save that data in the database.

In the above-given code, the index(), the named method, is a view function that is associated with the URL (\). It displays the template  index.html.

This index.html  template is the hompage  of the crud application. It contains the several links  by using them  we can add ,delete and view  the data saved in the database.

index.html 

The view function add()  is associated with the URL (/add). It renders the template add.html given below. It provides a basic form to enter the student information.

add.html

All the information entered by the admin is sent to the “save details” URL that is associated with the savedetails() methods. This method contains the code for the data entered by admin and store that data in the student table. It also produced several messages like data is saved successfully, and there is some error occurred.

It renders a success.html template in which a message is displayed to the admin. It also contains a link to view all records input by the users.

success.html

The method delete() is associated with the URL/delete. It displays the delete.html template that provides a form to the admin, which prompts to enter the student_id to delete that record of that specific student.It also contains a link to view all records input by the users.

Delete.html

The student_id entered by the admin is posted to the URL /deleterecord that  contains the python code to establish the connection with the database and then delete that record for the specified Student ID. The URL /deleterecord is associated with the method deleterecord() which is given below.

The method deleterecord() generates a message depending upon the condition whether the data is successfully deleted or any error occurred. It displayed an HTML template delete_record.html that shows a message to the admin.

delete_record.html

The template delete_record.html contains a link to the URL /view that shows the Student records to the admin.

It is associated with the method view() that establishes the connection with the database, fetches all the information, and passes that data to the view.html  template to display on the client side web-browser.

view.html

The complete python script is given below.

crud.py

Now run the server and execute the above-given python scripts using the following commands shown in the below screenshot.

  • Py –m  vene env
  • env\scripts\activate
  • Set Flask_app=crud.py
  • Flask run
Flask SQLite

Now copy  URL address from the cmd and paste it in the url section in the web-browser as given above.

Output

Flask SQLite

Now click on the add student record to add the record of the student.

Flask SQLite

Fill the details of the student and click on the submit button.

Flask SQLite

Now click on the View Students to view the student records.

Flask SQLite

Click on the “Go to home page” to navigate the home page.

Flask SQLite

Click on the “delete Record” button.

Now enter the student id to delete the record of that specific student and click on the submit button.

Flask SQLite

To confirm the delete operation is executed successfully, click on the View List button.

Flask SQLite

Pin It on Pinterest

Share This