Search code examples
db2ibm-cloudjupyter-notebookdashdbdata-science-experience

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


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.


Solution

  • 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.