JupySQL
JupySQL (version 0.11.1) enhances SQL interaction within Jupyter notebooks, allowing users to connect to various databases and execute queries directly using IPython magic commands. It supports advanced features like variable interpolation, caching, and data visualization. The project is actively maintained with frequent releases, typically every few weeks.
Common errors
-
ModuleNotFoundError: No module named 'jupysql'
cause The jupysql package is not installed in the current environment.fixRun `pip install jupysql` in your terminal or notebook environment. -
NameError: name 'sql' is not defined
cause The JupySQL IPython extension (`%load_ext sql`) has not been loaded, so the magic commands are not available.fixAdd `%load_ext sql` at the beginning of your notebook or session before using any `%sql` or `%%sql` commands. -
OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: database "mydb" does not exist
cause The database specified in the connection string does not exist, the server is not running, or connection details are incorrect.fixVerify that your database server is running, the database 'mydb' exists, and the connection string `%sql postgresql://user:password@localhost:5432/mydb` is correct. Also, ensure the necessary database driver (e.g., `psycopg2`) is installed via `pip install jupysql[postgres]`. -
TypeError: configure() got an unexpected keyword argument 'display_limit'
cause You are using `jupysql.configure` with an argument that was renamed in version 0.10.0.fixReplace `display_limit` with `result_limit` in your `jupysql.configure` call. If you were configuring styles, use `from jupysql import plot; plot.configure_styles(...)` instead.
Warnings
- breaking The `jupysql.configure` function's arguments `display_limit` and `style` were changed. `display_limit` was renamed to `result_limit`, and `style` was moved to `jupysql.plot.configure_styles`.
- gotcha JupySQL's primary functionality relies on IPython magic commands (`%sql`, `%%sql`). These require `%load_ext sql` to be run at the beginning of each new notebook or IPython session.
- gotcha JupySQL uses `{{variable}}` syntax for Python variable interpolation within SQL queries, which differs from standard f-strings or `.format()` methods.
Install
-
pip install jupysql -
pip install jupysql[postgres]
Imports
- %load_ext sql
import jupysql
%load_ext sql
- jupysql.configure
import jupysql jupysql.configure(result_limit=100)
- jupysql.plot
from jupysql import plot plot.configure_styles(template='ggplot')
Quickstart
# Load the JupySQL extension
%load_ext sql
# Connect to an in-memory SQLite database
%sql sqlite:///:memory:
# Create a table and insert some data
%%sql
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT,
department TEXT,
salary INTEGER
);
INSERT INTO employees (id, name, department, salary) VALUES
(1, 'Alice', 'Sales', 50000),
(2, 'Bob', 'Marketing', 60000),
(3, 'Charlie', 'Sales', 55000);
# Query the data
%sql SELECT * FROM employees WHERE department = 'Sales';