Skip to content

Commit

Permalink
adds example (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas authored Dec 30, 2022
1 parent 0d7ecc8 commit 2c987df
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions doc/howto.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,50 @@ FROM penguins.csv
GROUP BY species
ORDER BY count DESC
```

## Register SQLite UDF

To register a user-defined function (UDF) when using SQLite, you can use [SQLAlchemy's `@event.listens_for`](https://docs.sqlalchemy.org/en/14/dialects/sqlite.html#user-defined-functions) and SQLite's [`create_function`](https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_function):

### Install JupySQL

```{code-cell} ipython3
%pip install jupysql --quiet
```

### Create engine and register function

```{code-cell} ipython3
from sqlalchemy import create_engine
from sqlalchemy import event
def mysum(x, y):
return x + y
engine = create_engine("sqlite://")
@event.listens_for(engine, "connect")
def connect(conn, rec):
conn.create_function(name="MYSUM", narg=2, func=mysum)
```

### Create connection with existing engine

```{versionadded} 0.5.1
Pass existing engines to `%sql`
```

```{code-cell} ipython3
%load_ext sql
```

```{code-cell} ipython3
%sql engine
```

## Query

```{code-cell} ipython3
%%sql
SELECT MYSUM(1, 2)
```

0 comments on commit 2c987df

Please sign in to comment.