Create a gist now

Instantly share code, notes, and snippets.

@bmabey /
Last active Dec 11, 2015

What would you like to do?
Simple cell and line IPython Notebook magics for connecting to a ODBC DB and querying
from IPython.core.magic import (Magics, magics_class, line_magic,
cell_magic, line_cell_magic)
import pyodbc
import as sql
class SQLMagic(Magics):
con = None
def __init__(self, shell):
super(SQLMagic, self).__init__(shell)
def odbc(self, line):
"Connect to an ODBC database (DSN) configured in your odbc.ini"
self.con = pyodbc.connect('DSN='+line.strip())
def sql(self, line, cell=None):
"Run a SQL query"
query = None
if cell:
query = cell
query = line
df = sql.read_frame(query, self.con){'last_df': df})
return df
def load_ipython_extension(ip):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment