sql magic for IPython, hopefully evolving into full SQL client. http://catherinedevlin.blogspot.com/
 
 
Go to file
Catherine Devlin 410066e17b updated NEWS.txt 2013-03-29 10:36:49 -04:00
src Read configuration file 2013-03-29 10:30:27 -04:00
.gitignore Initial commit 2013-03-20 09:41:10 -07:00
HACKING.txt Skeleton as created by modern-package-template 2013-03-20 12:55:52 -04:00
MANIFEST.in Skeleton as created by modern-package-template 2013-03-20 12:55:52 -04:00
NEWS.txt updated NEWS.txt 2013-03-29 10:36:49 -04:00
README.md Cleanup RST documentation 2013-03-29 10:34:24 -04:00
README.rst Cleanup RST documentation 2013-03-29 10:34:24 -04:00
bootstrap.py Skeleton as created by modern-package-template 2013-03-20 12:55:52 -04:00
buildout.cfg Skeleton as created by modern-package-template 2013-03-20 12:55:52 -04:00
ipython-sql.wpr Skeleton as created by modern-package-template 2013-03-20 12:55:52 -04:00
ipython-sql.wpu Store .sql with query result 2013-03-29 08:49:00 -04:00
setup.py pyzmq no longer required 2013-03-29 08:50:17 -04:00

README.md

ipython-sql

Introduces a %sql / %%sql magic.

Connect to a database, using SQLAlchemy connect strings, then issue SQL commands within IPython or IPython Notebook.

Examples::

In [1]: %load_ext sql

In [2]: %%sql postgres://will:longliveliz@localhost/shakes
   ...: select * from character
   ...: where abbrev = 'ALICE'
   ...: 
Out[2]: [(u'Alice', u'Alice', u'ALICE', u'a lady attending on Princess Katherine', 22)]

In [3]: result = _

In [4]: print(result)
   charid        charname        abbrev      description    speechcount  
========================================================================
Alice          Alice          ALICE          a lady         22           
                                             attending on                
                                             Princess                    
                                             Katherine          
                                             
In [4]: result.keys
Out[5]: [u'charid', u'charname', u'abbrev', u'description', u'speechcount']

In [6]: result[0][0]
Out[6]: u'Alice'

In [7]: result[0]['description']
Out[7]: u'a lady attending on Princess Katherine'
                                             

After the first connection, connect info can be omitted::

In [8]: %sql select count(*) from work
Out[8]: [(43L,)]

Connections to multiple databases can be maintained. You can refer to an existing connection by username@database::

In [9]: %%sql will@shakes
   ...: select charname, speechcount from character 
   ...: where  speechcount = (select max(speechcount) 
   ...:                       from character);
   ...: 
Out[9]: [(u'Poet', 733)]

In [10]: print(_)
charname   speechcount 
======================
Poet       733  

Connecting

Connection strings are SQLAlchemy_ standard.

Some example connection strings::

mysql+pymysql://scott:tiger@localhost/foo
oracle://scott:tiger@127.0.0.1:1521/sidname
sqlite://
sqlite:///foo.db

.. _SQLAlchemy: http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls

Configuration

Query results are loaded as lists, so very large result sets may use up your systems memory. There is no autolimit by default.

You can stop text wrapping and set an autolimit by adding this to your ipython_config.py file::

c.SqlMagic.wrap = False
c.SqlMagic.autolimit = 1000 

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.

Development

https://github.com/catherinedevlin/ipython-sql

Credits

  • ipython-sqlitemagic_ for ideas
  • texttable_ for command-line table display
  • Matthias Bussonnier for help with configuration
  • Distribute_
  • Buildout_
  • modern-package-template_

.. _ipython-sqlitemagic: https://github.com/tkf/ipython-sqlitemagic .. _texttable: https://pypi.python.org/pypi/texttable .. _Buildout: http://www.buildout.org/ .. Distribute: http://pypi.python.org/pypi/distribute .. modern-package-template: http://pypi.python.org/pypi/modern-package-template