sqlite-minutils


Namesqlite-minutils JSON
Version 4.0.3 PyPI version JSON
download
home_pagehttps://github.com/AnswerDotAI/sqlite-minutils
SummaryA fork of sqlite-utils with CLI etc removed
upload_time2024-12-01 23:48:18
maintainerNone
docs_urlNone
authorJeremy Howard
requires_python>=3.9
licenseApache Software License 2.0
keywords nbdev jupyter notebook python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sqlite-minutils


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

> [!TIP]
>
> ### Where to find the complete documentation for this library
>
> If you want to learn about everything this project can do, we
> recommend reading the Python library section of the sqlite-utils
> project
> [here](https://sqlite-utils.datasette.io/en/stable/python-api.html).
>
> This project wouldn’t exist without Simon Willison and his excellent
> [sqlite-utils](https://github.com/simonw/sqlite-utils) project. Most
> of this project is his code, with some minor changes made to it.

## Install

    pip install sqlite-minutils

## Use

First, import the sqlite-miniutils library. Through the use of the
**all** attribute in our Python modules by using `import *` we only
bring in the `Database`, `Queryable`, `Table`, `View` classes. There’s
no risk of namespace pollution.

``` python
from sqlite_minutils.db import *
```

Then we create a SQLite database. For the sake of convienance we’re
doing it in-memory with the `:memory:` special string. If you wanted
something more persistent, name it something not surrounded by colons,
`data.db` is a common file name.

``` python
db = Database(":memory:")
```

Let’s drop (aka ‘delete’) any tables that might exist. These docs also
serve as a test harness, and we want to make certain we are starting
with a clean slate. This also serves as a handy sneak preview of some of
the features of this library.

``` python
for t in db.tables: t.drop()
```

User tables are a handy way to create a useful example with some
real-world meaning. To do this, we first instantiate the `users` table
object:

``` python
users = Table(db, 'Users')
users
```

    <Table Users (does not exist yet)>

The table doesn’t exist yet, so let’s add some columns via the
`Table.create` method:

``` python
users.create(columns=dict(id=int, name=str, age=int))
users
```

    <Table Users (id, name, age)>

What if we need to change the table structure?

For example User tables often include things like password field. Let’s
add that now by calling `create` again, but this time with
`transform=True`. We should now see that the `users` table now has the
`pwd:str` field added.

``` python
users.create(columns=dict(id=int, name=str, age=int, pwd=str), transform=True, pk='id')
users
```

    <Table Users (id, name, age, pwd)>

``` python
print(db.schema)
```

    CREATE TABLE "Users" (
       [id] INTEGER PRIMARY KEY,
       [name] TEXT,
       [age] INTEGER,
       [pwd] TEXT
    );

## Queries

Let’s add some users to query:

``` python
users.insert(dict(name='Raven', age=8, pwd='s3cret'))
users.insert(dict(name='Magpie', age=5, pwd='supersecret'))
users.insert(dict(name='Crow', age=12, pwd='verysecret'))
users.insert(dict(name='Pigeon', age=3, pwd='keptsecret'))
users.insert(dict(name='Eagle', age=7, pwd='s3cr3t'))
```

    <Table Users (id, name, age, pwd)>

A simple unfiltered select can be executed using `rows` property on the
table object.

``` python
users.rows
```

    <generator object Queryable.rows_where at 0x10849f6f0>

Let’s iterate over that generator to see the results:

``` python
[o for o in users.rows]
```

    [{'id': 1, 'name': 'Raven', 'age': 8, 'pwd': 's3cret'},
     {'id': 2, 'name': 'Magpie', 'age': 5, 'pwd': 'supersecret'},
     {'id': 3, 'name': 'Crow', 'age': 12, 'pwd': 'verysecret'},
     {'id': 4, 'name': 'Pigeon', 'age': 3, 'pwd': 'keptsecret'},
     {'id': 5, 'name': 'Eagle', 'age': 7, 'pwd': 's3cr3t'}]

Filtering can be done via the `rows_where` function:

``` python
[o for o in users.rows_where('age > 3')]
```

    [{'id': 1, 'name': 'Raven', 'age': 8, 'pwd': 's3cret'},
     {'id': 2, 'name': 'Magpie', 'age': 5, 'pwd': 'supersecret'},
     {'id': 3, 'name': 'Crow', 'age': 12, 'pwd': 'verysecret'},
     {'id': 5, 'name': 'Eagle', 'age': 7, 'pwd': 's3cr3t'}]

We can also `limit` the results:

``` python
[o for o in users.rows_where('age > 3', limit=2)]
```

    [{'id': 1, 'name': 'Raven', 'age': 8, 'pwd': 's3cret'},
     {'id': 2, 'name': 'Magpie', 'age': 5, 'pwd': 'supersecret'}]

The `offset` keyword can be combined with the `limit` keyword.

``` python
[o for o in users.rows_where('age > 3', limit=2, offset=1)]
```

    [{'id': 2, 'name': 'Magpie', 'age': 5, 'pwd': 'supersecret'},
     {'id': 3, 'name': 'Crow', 'age': 12, 'pwd': 'verysecret'}]

The `offset` must be used with `limit` or raise a `ValueError`:

``` python
try:
    [o for o in users.rows_where(offset=1)]
except ValueError as e:
    print(e)
```

    Cannot use offset without limit

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/AnswerDotAI/sqlite-minutils",
    "name": "sqlite-minutils",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "nbdev jupyter notebook python",
    "author": "Jeremy Howard",
    "author_email": "github@jhoward.fastmail.fm",
    "download_url": "https://files.pythonhosted.org/packages/39/de/25926f1caafee46ee4a7f617ab624652e6c44ba8b2351399c626b10553d4/sqlite-minutils-4.0.3.tar.gz",
    "platform": null,
    "description": "# sqlite-minutils\n\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\n> [!TIP]\n>\n> ### Where to find the complete documentation for this library\n>\n> If you want to learn about everything this project can do, we\n> recommend reading the Python library section of the sqlite-utils\n> project\n> [here](https://sqlite-utils.datasette.io/en/stable/python-api.html).\n>\n> This project wouldn\u2019t exist without Simon Willison and his excellent\n> [sqlite-utils](https://github.com/simonw/sqlite-utils) project. Most\n> of this project is his code, with some minor changes made to it.\n\n## Install\n\n    pip install sqlite-minutils\n\n## Use\n\nFirst, import the sqlite-miniutils library. Through the use of the\n**all** attribute in our Python modules by using `import *` we only\nbring in the `Database`, `Queryable`, `Table`, `View` classes. There\u2019s\nno risk of namespace pollution.\n\n``` python\nfrom sqlite_minutils.db import *\n```\n\nThen we create a SQLite database. For the sake of convienance we\u2019re\ndoing it in-memory with the `:memory:` special string. If you wanted\nsomething more persistent, name it something not surrounded by colons,\n`data.db` is a common file name.\n\n``` python\ndb = Database(\":memory:\")\n```\n\nLet\u2019s drop (aka \u2018delete\u2019) any tables that might exist. These docs also\nserve as a test harness, and we want to make certain we are starting\nwith a clean slate. This also serves as a handy sneak preview of some of\nthe features of this library.\n\n``` python\nfor t in db.tables: t.drop()\n```\n\nUser tables are a handy way to create a useful example with some\nreal-world meaning. To do this, we first instantiate the `users` table\nobject:\n\n``` python\nusers = Table(db, 'Users')\nusers\n```\n\n    <Table Users (does not exist yet)>\n\nThe table doesn\u2019t exist yet, so let\u2019s add some columns via the\n`Table.create` method:\n\n``` python\nusers.create(columns=dict(id=int, name=str, age=int))\nusers\n```\n\n    <Table Users (id, name, age)>\n\nWhat if we need to change the table structure?\n\nFor example User tables often include things like password field. Let\u2019s\nadd that now by calling `create` again, but this time with\n`transform=True`. We should now see that the `users` table now has the\n`pwd:str` field added.\n\n``` python\nusers.create(columns=dict(id=int, name=str, age=int, pwd=str), transform=True, pk='id')\nusers\n```\n\n    <Table Users (id, name, age, pwd)>\n\n``` python\nprint(db.schema)\n```\n\n    CREATE TABLE \"Users\" (\n       [id] INTEGER PRIMARY KEY,\n       [name] TEXT,\n       [age] INTEGER,\n       [pwd] TEXT\n    );\n\n## Queries\n\nLet\u2019s add some users to query:\n\n``` python\nusers.insert(dict(name='Raven', age=8, pwd='s3cret'))\nusers.insert(dict(name='Magpie', age=5, pwd='supersecret'))\nusers.insert(dict(name='Crow', age=12, pwd='verysecret'))\nusers.insert(dict(name='Pigeon', age=3, pwd='keptsecret'))\nusers.insert(dict(name='Eagle', age=7, pwd='s3cr3t'))\n```\n\n    <Table Users (id, name, age, pwd)>\n\nA simple unfiltered select can be executed using `rows` property on the\ntable object.\n\n``` python\nusers.rows\n```\n\n    <generator object Queryable.rows_where at 0x10849f6f0>\n\nLet\u2019s iterate over that generator to see the results:\n\n``` python\n[o for o in users.rows]\n```\n\n    [{'id': 1, 'name': 'Raven', 'age': 8, 'pwd': 's3cret'},\n     {'id': 2, 'name': 'Magpie', 'age': 5, 'pwd': 'supersecret'},\n     {'id': 3, 'name': 'Crow', 'age': 12, 'pwd': 'verysecret'},\n     {'id': 4, 'name': 'Pigeon', 'age': 3, 'pwd': 'keptsecret'},\n     {'id': 5, 'name': 'Eagle', 'age': 7, 'pwd': 's3cr3t'}]\n\nFiltering can be done via the `rows_where` function:\n\n``` python\n[o for o in users.rows_where('age > 3')]\n```\n\n    [{'id': 1, 'name': 'Raven', 'age': 8, 'pwd': 's3cret'},\n     {'id': 2, 'name': 'Magpie', 'age': 5, 'pwd': 'supersecret'},\n     {'id': 3, 'name': 'Crow', 'age': 12, 'pwd': 'verysecret'},\n     {'id': 5, 'name': 'Eagle', 'age': 7, 'pwd': 's3cr3t'}]\n\nWe can also `limit` the results:\n\n``` python\n[o for o in users.rows_where('age > 3', limit=2)]\n```\n\n    [{'id': 1, 'name': 'Raven', 'age': 8, 'pwd': 's3cret'},\n     {'id': 2, 'name': 'Magpie', 'age': 5, 'pwd': 'supersecret'}]\n\nThe `offset` keyword can be combined with the `limit` keyword.\n\n``` python\n[o for o in users.rows_where('age > 3', limit=2, offset=1)]\n```\n\n    [{'id': 2, 'name': 'Magpie', 'age': 5, 'pwd': 'supersecret'},\n     {'id': 3, 'name': 'Crow', 'age': 12, 'pwd': 'verysecret'}]\n\nThe `offset` must be used with `limit` or raise a `ValueError`:\n\n``` python\ntry:\n    [o for o in users.rows_where(offset=1)]\nexcept ValueError as e:\n    print(e)\n```\n\n    Cannot use offset without limit\n",
    "bugtrack_url": null,
    "license": "Apache Software License 2.0",
    "summary": "A fork of sqlite-utils with CLI etc removed",
    "version": "4.0.3",
    "project_urls": {
        "Homepage": "https://github.com/AnswerDotAI/sqlite-minutils"
    },
    "split_keywords": [
        "nbdev",
        "jupyter",
        "notebook",
        "python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "851278e39e4734bb8770955a6ec7f5dc1a918d56c8f94a1eaa2c7fb91f2fec2a",
                "md5": "8582fd852f4b51a0ceffffaca9b364b0",
                "sha256": "33254c37d88b8dfd6218ba0d7c4d64232a2bf234e15b841459c5676dea2b2db0"
            },
            "downloads": -1,
            "filename": "sqlite_minutils-4.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8582fd852f4b51a0ceffffaca9b364b0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 79288,
            "upload_time": "2024-12-01T23:48:16",
            "upload_time_iso_8601": "2024-12-01T23:48:16.776540Z",
            "url": "https://files.pythonhosted.org/packages/85/12/78e39e4734bb8770955a6ec7f5dc1a918d56c8f94a1eaa2c7fb91f2fec2a/sqlite_minutils-4.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "39de25926f1caafee46ee4a7f617ab624652e6c44ba8b2351399c626b10553d4",
                "md5": "e707e71fd6c83e8dd992722c897fdf4a",
                "sha256": "c3fc6d20f86b2cd0071eaa9e356024ef83aba3ce9d9606e50c4ab255c3279a71"
            },
            "downloads": -1,
            "filename": "sqlite-minutils-4.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "e707e71fd6c83e8dd992722c897fdf4a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 73036,
            "upload_time": "2024-12-01T23:48:18",
            "upload_time_iso_8601": "2024-12-01T23:48:18.746361Z",
            "url": "https://files.pythonhosted.org/packages/39/de/25926f1caafee46ee4a7f617ab624652e6c44ba8b2351399c626b10553d4/sqlite-minutils-4.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-01 23:48:18",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "AnswerDotAI",
    "github_project": "sqlite-minutils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sqlite-minutils"
}
        
Elapsed time: 0.50311s