How can I enable SQL Magics in Jupyter Notebooks on IBM Data Science Experience?

2.3k views Asked by At

I am using a Jupyter Notebook on IBM Data Science Experience. Is it possible to enable SQL Magics/IPython-sql? How can I install it?

I want to connect to dashDB/DB2 and run SQL statements.

2

There are 2 answers

0
data_henrik On BEST ANSWER

Yes, it is possible to use the IPython-sql (SQL Magics) module in the Jupyter Notebooks. The trick is to install it into the user space. Run the following in a code cell:

!pip install --user ipython-sql

If you want to connect to DB2 or dashDB, then you would need to install the related database drivers. Because the SQL Magics depend on SQLAlchemy, use these commands (same cell as the command above works):

!pip install --user ibm_db
!pip install --user ibm_db_sa

Once everything is installed, you need to load the SQL Magics extension:

%load_ext sql

I took the instructions on installing SQL Magics in the Data Science Experience from this blog post. It also has an example on how to connect to the database.

1
Torsten Steinbach On

There is also another way to run SQLs against dashDB from IBM Data Science Experience. It has already pre-deployed the ibmdbpy and ibmdbR libraries for Python and R notebooks, respectively. So you don't have to set up anything before using it.

Here is a sample for Python: https://apsportal.ibm.com/analytics/notebooks/5a59ba9b-02b2-40e4-b955-9727cb68c88b/view?access_token=09240b783432f1a62004bcc82b48a7aed07afc401e2f94a77c7e087b74d8c053

And here is one for R: https://apsportal.ibm.com/analytics/notebooks/4ff39dad-f497-40c6-941c-43162c347819/view?access_token=9b2ae23b8ec4d8223a7f88950db66a72c736b269ef6cf1d658bb1fcd49c78f35