catherinedevlin / ipython-sql

%%sql magic for IPython, hopefully evolving into full SQL client
MIT License
1.79k stars 368 forks source link

Reuse existing connection object or accept one #28

Open ckald opened 9 years ago

ckald commented 9 years ago

Hi. I am using ipython-sql as an easy-to-access SQL terminal, but in other cases I use the peewee ORM. This leads to having 2 open database connections and some temporal discrepancies between the state of my SQLite DB seen through 2 connections.

I would like to be able to initialise %sql magic with existing connection object

conn = other.module.database('sqlite:///')
%sql :conn

and/or reuse a newly opened connection:

conn = %sql sqlite:///
other.module.database(connection=conn)

Is this possible?

pfmoore commented 4 years ago

I would also be interested in this. The issue has been around for quite a time now, is there any maintainer interest in this feature? Would a PR implementing something like this be accepted? (I'm not an expert in Jupyter internals, so I don't know if I'd be able to write such a PR, but I'd be willing to try if the project owners were interested in it...)

idomic commented 1 year ago

This has been added to JupySQL 0.5.1 (a fork of ipython-sql):

pip install jupysql --upgrade
import pandas as pd
from sqlalchemy.engine import create_engine

engine = create_engine("sqlite://")

df = pd.DataFrame({"x": range(5)})
df.to_sql("numbers", engine)
%load_ext SQL
%sql engine
%%sql
SELECT * FROM numbers