DEV Community

Cover image for Python + Flask - Part 3 - Local Database
LUCIANO DE SOUSA PEREIRA
LUCIANO DE SOUSA PEREIRA

Posted on • Originally published at lucianopereira.netlify.com

Python + Flask - Part 3 - Local Database

titulo

This is the third part of a series about the Flask framework, a common tool used to create web applications with Python.

Objectives

The part 3 will focus on connecting the web API with a local database.
The full example is available here: Python-Flask.

Topics

Local Database

Create another Python file inside the api folder called db_api and write this code:

flask24_0

The functions below must be created as well:

  • Creates the database

flask24

  • Creates tables

flask25

  • Executes the operation

flask27

  • Starts the database. This function must be called before app.run().

flask26

Finally, run the following command:

python .\api\db_api.py

The test.db will be created inside the api folder and will contain the user table:

flask28

Copy and modify the HTTP functions from api.py to db_api.py.

  • POST

flask29

  • PUT

flask30

  • GET

flask31

  • DELETE

flask32

Test each one and see the result.

Next

We will connect our web API with a remote database.

Top comments (0)