RDBMS access via IPython
|Author:||Catherine Devlin, http://catherinedevlin.blogspot.com|
Introduces a %sql (or %%sql) magic.
Connect to a database, using SQLAlchemy connect strings, then issue SQL commands within IPython or IPython Notebook.
In : %load_ext sql In : %%sql postgresql://will:longliveliz@localhost/shakes ...: select * from character ...: where abbrev = 'ALICE' ...: Out: [(u'Alice', u'Alice', u'ALICE', u'a lady attending on Princess Katherine', 22)] In : result = _ In : print(result) charid charname abbrev description speechcount ================================================================================= Alice Alice ALICE a lady attending on Princess Katherine 22 In : result.keys Out: [u'charid', u'charname', u'abbrev', u'description', u'speechcount'] In : result Out: u'Alice' In : result.description Out: u'a lady attending on Princess Katherine'
After the first connection, connect info can be omitted:
In : %sql select count(*) from work Out: [(43L,)]
Connections to multiple databases can be maintained. You can refer to an existing connection by username@database:
In : %%sql will@shakes ...: select charname, speechcount from character ...: where speechcount = (select max(speechcount) ...: from character); ...: Out: [(u'Poet', 733)] In : print(_) charname speechcount ====================== Poet 733
You may use multiple SQL statements inside a single cell, but you will only see any query results from the last of them, so this really only makes sense for statements with no output:
In : %%sql sqlite:// ....: CREATE TABLE writer (first_name, last_name, year_of_death); ....: INSERT INTO writer VALUES ('William', 'Shakespeare', 1616); ....: INSERT INTO writer VALUES ('Bertold', 'Brecht', 1956); ....: Out: 
Bind variables (bind parameters) can be used in the “named” (:x) style. The variable names used should be defined in the local namespace:
In : name = 'Countess' In : %sql select description from character where charname = :name Out: [(u'mother to Bertram',)]
Connection strings are SQLAlchemy standard.
Some example connection strings:
mysql+pymysql://scott:tiger@localhost/foo oracle://scott:firstname.lastname@example.org:1521/sidname sqlite:// sqlite:///foo.db
Note that mysql and mysql+pymysql connections (and perhaps others) don’t read your client character set information from .my.cnf. You need to specify it in the connection string:
Query results are loaded as lists, so very large result sets may use up your system’s memory. There is no autolimit by default.
You can set an autolimit by adding this to your ipython_config.py file:
c.SqlMagic.autolimit = 1000
You can similarly change the table printing style to any of prettytable’s defined styles (currently DEFAULT, MSWORD_FRIENDLY, PLAIN_COLUMNS, RANDOM):
c.SqlMagic.style = 'PLAIN_COLUMNS'
You can create and find your ipython_config.py file from the command line:
ipython profile create ipython locate profile
See http://ipython.org/ipython-doc/stable/config/overview.html#configuration-objects-and-files for more details on IPython configuration.
Once your data is in IPython, you may want to manipulate it with Pandas:
In : import pandas as pd In : result = %sql SELECT * FROM character WHERE speechcount > 25 In : dataframe = pd.DataFrame(result, columns=result.keys)
Install the lastest release with:
pip install ipython-sql
or download from https://github.com/catherinedevlin/ipython-sql and:
cd ipython-sql sudo python setup.py install
Release date: 21-Mar-2013
- Initial release
Release date: 29-Mar-2013
- Release to PyPI
- Results returned as lists
- print(_) to get table form in text console
- set autolimit and text wrap in configuration
Release date: 29-Mar-2013
- Python 3 compatibility
- use prettyprint package
- allow multiple SQL per cell
Release date: 30-May-2013
- Accept bind variables (Thanks Mike Wilson!)
Release date: 15-June-2013
- Recognize socket connection strings
- Bugfix - issue 4 (remember existing connections by case)
Release date: 30-July-2013
Converted from an IPython Plugin to an Extension for 1.0 compatibility
Release date: 01-Aug-2013
Deleted Plugin import left behind in 0.2.2
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size ipython_sql-0.2.2.1-py2.7.egg (10.3 kB)||File type Egg||Python version 2.7||Upload date||Hashes View|
|Filename, size ipython-sql-0.2.2.1.tar.gz (6.3 kB)||File type Source||Python version None||Upload date||Hashes View|