[IPython-User] SQL magic functions

Christopher Brooks cab938@mail.usask...
Sun Jun 13 20:59:27 CDT 2010


Often I need to insert and remove things from a DB while doing data 
analysis.  I use normal python db apis for this, but I recently wrapped 
a couple in magic functions within ipython to make it a bit more 
natural.  Nothing too fancy here, but it makes the simple routines of 
retrieving and inserting data easy...

Script attached for those interested (mysql only atm), examples below...

Chris

Definition:	sql(self, arg)
Docstring:
     Provides simplified syntax for operating with databases.  Query
types supported include SELECT, INSERT, and DELETE.  Initialization of 
SQL connection must be done first, for instance:
         sql_db=MySQLdb.connect("host","user","pass","schema")
     Usage examples:
         %sql select * from data
         %sql describe data
         %sql insert into data values ("a","b","c")
         %sql insert into data myvar
             if myvar is a dict this expands to "insert into data 
(key1...keyn) values (val1...valn)
         %sql create table data myvar"
           if myvar is a dict this creates the table with columns as 
keys and datatypes as values
           e.g. myvar = {'col1': 'VARCHAR(255)', 'col2': 'INT'}
     See also:
         %raw_sql

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ipy_sql.ipy
Url: http://mail.scipy.org/pipermail/ipython-user/attachments/20100613/7178577e/attachment.pl 


More information about the IPython-User mailing list