First, we need to create a connection with database:
To create a connection, it's needed to invoke the function connect() from sqlite3, and save it in a variable.
In this case the connection was saved in conn, this variable will receive all the queries.
After running this on python3 interactive shell we will receive the version of sqlite library and the database will be generated:
$ python3
>>> from create_connection import create_connection
>>> create_connection('my_database.db')
2.6.0
>>>
If you pass file name as ':memory:', the database will be saved on the memory of computer:
To add data to the tables, we need the function execute of the Cursor object. The execute function receive two params, the sql statement and a tuple with the data, after this, we need to run the commit function of the connection, something like this:
Let's create the function to add data on projects table and the tasks tables with this logic:
Running the python3 interactive shell, import the class and instantiate it in the db_manager variable:
after that, create the variable containing the project data, and a variable to save the project id on database:
then, create two tasks and save it on the database:
The project and the tasks are saved now. Open the database to see the data.
With the open database, use this commands to format the output:
Use the SELECT statement to get data from projects table:
Use the same statement to get data from tasks table: