Prerequisites
Before getting started, you must have the following software installed locally:- JupySQL:
pip install jupysql - Jupyterlab:
pip install jupyterlab - SKlearn Evaluation:
pip install sklearn-evaluation - Python
- pymysql:
pip install pymysql
NOTE
Once you have the above requirements fulfilled, you can open Jupyter lab simply by calling jupyterlab - this will open the notebook interface.
If Jupyter lab is already running in a notebook, you can simply run the cell bellow to get the dependencies.
NOTE You may need to restart the kernel to use updated packages.
NOTE You will need to adjust the connection string according to the instance type you are trying to connect to (url, user, and password). The example below uses a local instance.
Connecting to the database via JupySQL
In this example, a docker instance is used, and that is reflecting the data in the connection string. Theroot user is used to connect to the local instance, create a database, and check that data can actually be read from and written into the table.
Saving and loading queries
Now after you create a database, you can write some sample data into it and query it. JupySQL allows you to break queries into multiple cells, simplifying the process of building large queries. You can write complex queries, save them, and execute them when needed, in a similar manner to CTEs in SQL.
NOTE
--save stores the query, not the data.
Note that we are using --with;, this will retrieve previously saved queries, and prepend them (using CTEs). Then, we save the query in track_fav.