SQL Magic extension for Oracle and IPython

  • An example of how to query Oracle from IPython using SQL magic extensions
  • This is a straighforward interface for running SQL
  • The results integrate easily into the python environment and with pandas
  • Dependencies: needs an Oracle client installation, cx_Oracle and ipython-sql

Connect to Oracle and run a query using %SQL magic extensions

  • SQL magic extensions introduce the %%sql cell magic and %sql line magic for running SQL in IPython
  • The connect string to the database uses sql alchemy syntax

In [1]:
# loads the SQL magic extensions
%load_ext sql

In [2]:
# Connect to Oracle
%sql oracle+cx_oracle://scott:tiger@dbserver:1521/?service_name=orcl.mydomain.com


Out[2]:
u'Connected: system@'

In [3]:
%%sql 
select * from emp


0 rows affected.
Out[3]:
empno ename job mgr hiredate sal comm deptno
7902 FORD ANALYST 7566 1981-12-03 00:00:00 3000 None 20
7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500 0 30
7698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850 None 30
7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 1250 1400 30
7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 None 20
7566 JONES MANAGER 7839 1981-04-02 00:00:00 2975 None 20
7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 None 20
7839 KING PRESIDENT None 1981-11-17 00:00:00 5000 None 10
7521 WARD SALESMAN 7698 1981-02-22 00:00:00 1250 500 30
7934 MILLER CLERK 7782 1982-01-23 00:00:00 1300 None 10
7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 1600 300 30
7900 JAMES CLERK 7698 1981-12-03 00:00:00 950 None 30
7782 CLARK MANAGER 7839 1981-06-09 00:00:00 2450 None 10
7876 ADAMS CLERK 7788 1987-05-23 00:00:00 1100 None 20

Bind variables and DML


In [4]:
Employee_name="SCOTT"

%sql select * from emp where ename=:Employee_name


0 rows affected.
Out[4]:
empno ename job mgr hiredate sal comm deptno
7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 None 20

In [5]:
%sql update emp set sal=3500 where ename=:Employee_name
%sql commit
%sql select * from emp where ename=:Employee_name


1 rows affected.
Done.
0 rows affected.
Out[5]:
empno ename job mgr hiredate sal comm deptno
7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3500 None 20

Additional example of the integration with the IPython environment


In [6]:
myResultSet=%sql select ename "Employee Name", sal "Salary" from emp

%matplotlib inline
import matplotlib
matplotlib.style.use('ggplot')

myResultSet.bar()


0 rows affected.
Out[6]:
<Container object of 14 artists>

Integration with Pandas

This opens many additional possibilities for data analysis


In [7]:
%%sql
select e1.ename "Employee Name", e1.job "Job", e2.ename "Manager Name" 
from emp e1, emp e2
where e1.mgr = e2.empno(+)


0 rows affected.
Out[7]:
Employee Name Job Manager Name
SMITH CLERK FORD
JAMES CLERK BLAKE
ALLEN SALESMAN BLAKE
WARD SALESMAN BLAKE
MARTIN SALESMAN BLAKE
TURNER SALESMAN BLAKE
ADAMS CLERK SCOTT
SCOTT ANALYST JONES
FORD ANALYST JONES
CLARK MANAGER KING
JONES MANAGER KING
BLAKE MANAGER KING
MILLER CLERK CLARK
KING PRESIDENT None

In [8]:
# save result set into my_ResultSet and copy it to pandas in my_DataFrame
my_ResultSet = _

my_DataFrame=my_ResultSet.DataFrame()

my_DataFrame.head()


Out[8]:
Employee Name Job Manager Name
0 SMITH CLERK FORD
1 JAMES CLERK BLAKE
2 ALLEN SALESMAN BLAKE
3 WARD SALESMAN BLAKE
4 MARTIN SALESMAN BLAKE

In [ ]: