https://github.com/fastai/fastsql1
A bit of extra usability for sqlalchemy v2.
https://github.com/fastai/fastsql1
Last synced: 19 days ago
JSON representation
A bit of extra usability for sqlalchemy v2.
- Host: GitHub
- URL: https://github.com/fastai/fastsql1
- Owner: fastai
- License: apache-2.0
- Created: 2019-07-21T23:43:07.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-27T17:03:44.000Z (over 1 year ago)
- Last Synced: 2025-04-23T15:03:30.133Z (9 months ago)
- Language: Jupyter Notebook
- Homepage: https://fastai.github.io/fastsql
- Size: 366 KB
- Stars: 77
- Watchers: 2
- Forks: 12
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# fastsql
A bit of extra usability for sqlalchemy v2.
## Install
pip install fastsql
## Example
This little library provides a single function,
[`conn_db`](https://fastai.github.io/fastsql/core.html#conn_db), which
returns an extended sqlalchemy `MetaData` object which you can use for
accessing your database with full dynamic autocomplete support in
Jupyter and IPython. So it’s particularly useful for interactive
development.
We demonstrate it here using the ‘chinook’ sample database.
``` python
from fastsql import conn_db
from fastcore.utils import *
```
``` python
url = 'https://github.com/lerocha/chinook-database/raw/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite'
path = Path('chinook.sqlite')
if not path.exists(): urlsave(url, path)
```
``` python
connstr = f"sqlite:///{path}"
db = conn_db(connstr)
```
``` python
' '.join(db.tables)
```
'Album Artist Customer Employee Genre Invoice InvoiceLine Track MediaType Playlist PlaylistTrack'
``` python
a = db.Album
```
``` python
list(a.c)
```
[Column('AlbumId', INTEGER(), table=, primary_key=True, nullable=False),
Column('Title', NVARCHAR(length=160), table=, nullable=False),
Column('ArtistId', INTEGER(), ForeignKey('Artist.ArtistId'), table=, nullable=False)]
Rows are returned as named tuples.
``` python
rs = db.sql('select AlbumId,Title from Album')
rs[0]
```
Row(AlbumId=1, Title='For Those About To Rock We Salute You')
``` python
a.get(a.c.Title.startswith('F'), limit=5)
```
[Row(AlbumId=1, Title='For Those About To Rock We Salute You', ArtistId=1),
Row(AlbumId=7, Title='Facelift', ArtistId=5),
Row(AlbumId=60, Title='Fireball', ArtistId=58),
Row(AlbumId=88, Title='Faceless', ArtistId=87),
Row(AlbumId=99, Title='Fear Of The Dark', ArtistId=90)]
``` python
db.close()
```