Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/06. Databases and SQL for Data Science with Python/04. Accessing Databases using Python/03. Accessing Databases with SQL Magic.ipynb
Views: 4598
Kernel: Python 3
Accessing Databases with SQL Magic
Objectives
After completing this lab you will be able to:
Perform simplified database access using SQL "magic"
To communicate with SQL Databases from within a JupyterLab notebook, we can use the SQL "magic" provided by the ipython-sql extension. "Magic" is JupyterLab's term for special commands that start with "%". Below, we'll use the load___ext magic to load the ipython-sql extension. In the lab environemnt provided in the course the ipython-sql extension is already installed and so is the ibm_db_sa driver.
In [17]:
Collecting sqlalchemy==1.4.17
Downloading SQLAlchemy-1.4.17-cp38-cp38-win_amd64.whl (1.5 MB)
Collecting greenlet!=0.4.17; python_version >= "3"
Downloading greenlet-1.1.0-cp38-cp38-win_amd64.whl (96 kB)
Installing collected packages: greenlet, sqlalchemy
Attempting uninstall: greenlet
Found existing installation: greenlet 0.4.17
Uninstalling greenlet-0.4.17:
Successfully uninstalled greenlet-0.4.17
Attempting uninstall: sqlalchemy
Found existing installation: SQLAlchemy 1.3.9
Uninstalling SQLAlchemy-1.3.9:
Successfully uninstalled SQLAlchemy-1.3.9
Successfully installed greenlet-1.1.0 sqlalchemy-1.4.17
Requirement already satisfied: ibm_db_sa in c:\users\dabarnes\anaconda3\lib\site-packages (0.3.6)
Requirement already satisfied: sqlalchemy>=0.7.3 in c:\users\dabarnes\anaconda3\lib\site-packages (from ibm_db_sa) (1.4.17)
Requirement already satisfied: ibm-db>=2.0.0 in c:\users\dabarnes\anaconda3\lib\site-packages (from ibm_db_sa) (3.0.4)
Requirement already satisfied: greenlet!=0.4.17; python_version >= "3" in c:\users\dabarnes\anaconda3\lib\site-packages (from sqlalchemy>=0.7.3->ibm_db_sa) (1.1.0)
Requirement already satisfied: ipython-sql in c:\users\dabarnes\anaconda3\lib\site-packages (0.4.0)
Requirement already satisfied: sqlparse in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython-sql) (0.4.1)
Requirement already satisfied: sqlalchemy>=0.6.7 in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython-sql) (1.4.17)
Requirement already satisfied: ipython>=1.0 in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython-sql) (7.19.0)
Requirement already satisfied: ipython-genutils>=0.1.0 in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython-sql) (0.2.0)
Requirement already satisfied: six in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython-sql) (1.15.0)
Requirement already satisfied: prettytable<1 in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython-sql) (0.7.2)
Requirement already satisfied: greenlet!=0.4.17; python_version >= "3" in c:\users\dabarnes\anaconda3\lib\site-packages (from sqlalchemy>=0.6.7->ipython-sql) (1.1.0)
Requirement already satisfied: jedi>=0.10 in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython>=1.0->ipython-sql) (0.17.1)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython>=1.0->ipython-sql) (3.0.8)
Requirement already satisfied: setuptools>=18.5 in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython>=1.0->ipython-sql) (50.3.1.post20201107)
Requirement already satisfied: traitlets>=4.2 in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython>=1.0->ipython-sql) (5.0.5)
Requirement already satisfied: pickleshare in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython>=1.0->ipython-sql) (0.7.5)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython>=1.0->ipython-sql) (0.4.4)
Requirement already satisfied: backcall in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython>=1.0->ipython-sql) (0.2.0)
Requirement already satisfied: pygments in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython>=1.0->ipython-sql) (2.7.2)
Requirement already satisfied: decorator in c:\users\dabarnes\anaconda3\lib\site-packages (from ipython>=1.0->ipython-sql) (4.4.2)
Requirement already satisfied: parso<0.8.0,>=0.7.0 in c:\users\dabarnes\anaconda3\lib\site-packages (from jedi>=0.10->ipython>=1.0->ipython-sql) (0.7.0)
Requirement already satisfied: wcwidth in c:\users\dabarnes\anaconda3\lib\site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=1.0->ipython-sql) (0.2.5)
In [18]:
The sql extension is already loaded. To reload it, use:
%reload_ext sql
Now we have access to SQL magic. With our first SQL magic command, we'll connect to a Db2 database. However, in order to do that, you'll first need to retrieve or create your credentials to access your Db2 database.
In [19]:
DB2/LINUXX8664
For convenience, we can use %%sql (two %'s instead of one) at the top of a cell to indicate we want the entire cell to be treated as SQL. Let's use this to create a table and fill it with some test data for experimenting.
In [7]:
* ibm_db_sa://kfm42587:***@dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net:50000/BLUDB
Done.
99 rows affected.
[]
Using Python Variables in your SQL Statements
You can use python variables in your SQL statements by adding a ":" prefix to your python variable names.
For example, if I have a python variable country
with a value of "Canada"
, I can use this variable in a SQL query to find all the rows of students from Canada.
In [20]:
* ibm_db_sa://kfm42587:***@dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net:50000/BLUDB
Done.
Assigning the Results of Queries to Python Variables
You can use the normal python assignment syntax to assign the results of your queries to python variables.
For example, I have a SQL query to retrieve the distribution of test scores (i.e. how many students got each score). I can assign the result of this query to the variable test_score_distribution
using the =
operator.
In [59]:
* ibm_db_sa://kfm42587:***@dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net:50000/BLUDB
Done.
Converting Query Results to DataFrames
You can easily convert a SQL query result to a pandas dataframe using the DataFrame()
method. Dataframe objects are much more versatile than SQL query result objects. For example, we can easily graph our test score distribution after converting to a dataframe.
In [60]:
Now you know how to work with Db2 from within JupyterLab notebooks using SQL "magic"!
In [61]:
* ibm_db_sa://kfm42587:***@dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net:50000/BLUDB
Done.