# fastsql
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
## Install
``` sh
pip install fastsql
```
## How to use
``` python
from fastsql import *
from dataclasses import dataclass
```
First we instantiate our database using FastSQL’s Database class:
``` python
db = Database("sqlite:///:memory:")
```
We demonstrate fastsql’s features here using dataclasses-as-schema
inspired by FastHTML’s [adv_app
example](https://github.com/AnswerDotAI/fasthtml/blob/main/examples/adv_app.py).
``` python
@dataclass
class User:
name:str
pwd:str
@dataclass
class Todo:
title:str
name:str
done:bool=False
details:str=''
id:int=None
```
Equipped with our schemas, let’s turn them into database tables.
``` python
users = db.create(User, pk='name')
todos = db.create(Todo, pk='id')
```
Let’s confirm the table design.
``` python
db.print_schema()
```
Table: Todo
- *id: INTEGER
- title: VARCHAR
- name: VARCHAR
- done: BOOLEAN
- details: VARCHAR
Table: User
- *name: VARCHAR
- pwd: VARCHAR
Does a table exist?
``` python
users.exists()
```
True
## Manipulating data
> Creating, reading, updating, and deleting records in database tables.
Let’s create some dataclass objects representing users and todos.
``` python
u0 = User('jph','foo')
u1 = User('rlt','bar')
t0 = Todo('do it', 'jph')
t1 = Todo('build it', 'jph')
t2 = Todo('write book', 'rlt')
```
Let’s convert these dataclass objects into database records. To do that
we insert them into their tables using the aply named `insert` method:
``` python
users.insert(u0)
users.insert(u1)
todos.insert(t0)
todos.insert(t1)
todos.insert(t2)
```
Todo(title='write book', name='rlt', done=False, details='', id=3)
Display all the user records.
``` python
for user in users():
print(user)
```
User(name='jph', pwd='foo')
User(name='rlt', pwd='bar')
Use where statement to filter records, in this case only jph’s todos.
``` python
for todo in todos(where="name = :name", name="jph"):
print(todo)
```
Todo(title='do it', name='jph', done=False, details='', id=1)
Todo(title='build it', name='jph', done=False, details='', id=2)
Look only for those records with the word `it` in it.
``` python
for todo in todos(where="title LIKE :title", title="%% it%%"):
print(todo)
```
Todo(title='do it', name='jph', done=False, details='', id=1)
Todo(title='build it', name='jph', done=False, details='', id=2)
Fetch a record just by the primary key.
``` python
user = users['rlt']
user
```
User(name='rlt', pwd='bar')
Change a value in a record.
``` python
user.pwd = 'baz'
users.update(user)
users['rlt']
```
User(name='rlt', pwd='baz')
Raw data
{
"_id": null,
"home_page": "https://github.com/answerdotai/fastsql",
"name": "fastsql",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "nbdev jupyter notebook python",
"author": "Jeremy Howard and Daniel Roy Greenfeld",
"author_email": "daniel@feldroy.com",
"download_url": "https://files.pythonhosted.org/packages/80/80/f3eeb0213bfc1db1be5e4c6818230b36dc1b97e5d8b57f3d85449251794e/fastsql-2.0.3.tar.gz",
"platform": null,
"description": "# fastsql\n\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\n## Install\n\n``` sh\npip install fastsql\n```\n\n## How to use\n\n``` python\nfrom fastsql import *\nfrom dataclasses import dataclass\n```\n\nFirst we instantiate our database using FastSQL\u2019s Database class:\n\n``` python\ndb = Database(\"sqlite:///:memory:\")\n```\n\nWe demonstrate fastsql\u2019s features here using dataclasses-as-schema\ninspired by FastHTML\u2019s [adv_app\nexample](https://github.com/AnswerDotAI/fasthtml/blob/main/examples/adv_app.py).\n\n``` python\n@dataclass\nclass User:\n name:str\n pwd:str\n\n@dataclass\nclass Todo:\n title:str\n name:str\n done:bool=False\n details:str=''\n id:int=None\n```\n\nEquipped with our schemas, let\u2019s turn them into database tables.\n\n``` python\nusers = db.create(User, pk='name')\ntodos = db.create(Todo, pk='id')\n```\n\nLet\u2019s confirm the table design.\n\n``` python\ndb.print_schema()\n```\n\n Table: Todo\n - *id: INTEGER\n - title: VARCHAR\n - name: VARCHAR\n - done: BOOLEAN\n - details: VARCHAR\n Table: User\n - *name: VARCHAR\n - pwd: VARCHAR\n\nDoes a table exist?\n\n``` python\nusers.exists()\n```\n\n True\n\n## Manipulating data\n\n> Creating, reading, updating, and deleting records in database tables.\n\nLet\u2019s create some dataclass objects representing users and todos.\n\n``` python\nu0 = User('jph','foo')\nu1 = User('rlt','bar')\nt0 = Todo('do it', 'jph')\nt1 = Todo('build it', 'jph')\nt2 = Todo('write book', 'rlt')\n```\n\nLet\u2019s convert these dataclass objects into database records. To do that\nwe insert them into their tables using the aply named `insert` method:\n\n``` python\nusers.insert(u0)\nusers.insert(u1)\ntodos.insert(t0)\ntodos.insert(t1)\ntodos.insert(t2)\n```\n\n Todo(title='write book', name='rlt', done=False, details='', id=3)\n\nDisplay all the user records.\n\n``` python\nfor user in users():\n print(user)\n```\n\n User(name='jph', pwd='foo')\n User(name='rlt', pwd='bar')\n\nUse where statement to filter records, in this case only jph\u2019s todos.\n\n``` python\nfor todo in todos(where=\"name = :name\", name=\"jph\"):\n print(todo)\n```\n\n Todo(title='do it', name='jph', done=False, details='', id=1)\n Todo(title='build it', name='jph', done=False, details='', id=2)\n\nLook only for those records with the word `it` in it.\n\n``` python\nfor todo in todos(where=\"title LIKE :title\", title=\"%% it%%\"):\n print(todo)\n```\n\n Todo(title='do it', name='jph', done=False, details='', id=1)\n Todo(title='build it', name='jph', done=False, details='', id=2)\n\nFetch a record just by the primary key.\n\n``` python\nuser = users['rlt']\nuser\n```\n\n User(name='rlt', pwd='bar')\n\nChange a value in a record.\n\n``` python\nuser.pwd = 'baz'\nusers.update(user)\nusers['rlt']\n```\n\n User(name='rlt', pwd='baz')\n",
"bugtrack_url": null,
"license": "Apache Software License 2.0",
"summary": "A MiniDataAPI spec implementation for SQLAlchemy V2",
"version": "2.0.3",
"project_urls": {
"Homepage": "https://github.com/answerdotai/fastsql"
},
"split_keywords": [
"nbdev",
"jupyter",
"notebook",
"python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2ba3ee4f17a7d48e6e4a0407c4dd0bd8f3c660a64b6a8e0b6afc25cd6e887b4f",
"md5": "9a95af57879147ef90c8790b447ff75f",
"sha256": "d674ba9cd9f42357ade3aa8ee6a5a54fb4ded714d5fd0a085bdf5c0c48d86958"
},
"downloads": -1,
"filename": "fastsql-2.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9a95af57879147ef90c8790b447ff75f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 10498,
"upload_time": "2024-08-15T17:20:43",
"upload_time_iso_8601": "2024-08-15T17:20:43.820847Z",
"url": "https://files.pythonhosted.org/packages/2b/a3/ee4f17a7d48e6e4a0407c4dd0bd8f3c660a64b6a8e0b6afc25cd6e887b4f/fastsql-2.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8080f3eeb0213bfc1db1be5e4c6818230b36dc1b97e5d8b57f3d85449251794e",
"md5": "b365f0cf0a41d3d506a57d8c800285ba",
"sha256": "e0fb504335792a763ed7d76bf53d677b51fd6a8591885c2d6381d6bb061e60c2"
},
"downloads": -1,
"filename": "fastsql-2.0.3.tar.gz",
"has_sig": false,
"md5_digest": "b365f0cf0a41d3d506a57d8c800285ba",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 11147,
"upload_time": "2024-08-15T17:20:45",
"upload_time_iso_8601": "2024-08-15T17:20:45.284379Z",
"url": "https://files.pythonhosted.org/packages/80/80/f3eeb0213bfc1db1be5e4c6818230b36dc1b97e5d8b57f3d85449251794e/fastsql-2.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-15 17:20:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "answerdotai",
"github_project": "fastsql",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fastsql"
}