Skip to main content

An improved Python interface to SQLite

Project description

isqlite

isqlite is an improved Python interface to SQLite. It has a more convenient API, support for database migrations, and a command-line interface.

Features

  • An improved Python API.
    • e.g., db.create("people", {"name": "John Doe"}) instead of cursor.execute("INSERT INTO people VALUES ('John Doe')")
    • Rows are returned as OrderedDict objects instead of tuples.
    • Helper methods to simplify common patterns, e.g. get_or_create.
  • Database migrations.
    • Automatically diff the database against a schema defined in Python and apply the results.
    • Or, manually alter the database schema from the command line using commands like isqlite drop-table and isqlite rename-column.
  • A command-line interface.

Usage

Python interface

from isqlite import Database

with Database(":memory:") as db:
    # Create a new row in the database.
    pk = db.create("employees", {"name": "John Doe", "age": 30})

    # Retrieve the row as an OrderedDict.
    person = db.get_by_pk("employees", pk)
    print(person["name"], person["age"])

    # Update the row.
    db.update_by_pk("employees", pk, {"age": 35})

    # Delete the row.
    db.delete_by_pk("employees", pk)

    # Filter rows with a query.
    employees = db.list(
        "employees",
        where="name LIKE :name_pattern AND age > 40",
        values={"name_pattern": "John%"},
    )

    # Use raw SQL if necessary.
    pairs = db.sql(
        """
        SELECT
          teams.name, employees.name
        FROM
          employees
        INNER JOIN
          teams
        ON
          employees.team = teams.id
        """
    )

Database migrations

Automated

In schema.py (the exact name of the file does not matter, but for the command-line tool to work the schema must be defined in a variable called SCHEMA):

from base.sql import ForeignKeyColumn, IntegerColumn, Table, TextColumn

SCHEMA = [
    Table(
        "books",
        columns=[
            TextColumn("title", required=True),
            ForeignKeyColumn("author", model="authors", required=True),
            IntegerColumn("pages", required=False),
        ],
    ),
    Table(
        "authors",
        columns=[
            TextColumn("name", required=True),
        ],
    ),
]

On the command-line (assuming your database is in db.sqlite3):

$ isqlite --db db.sqlite3 --schema schema.py migrate

The isqlite migrate command will compare the database file to the Python schema, and print out the changes required to make the database match the schema. To apply the changes, run isqlite migrate again with the --write flag.

Manual

The isqlite command-line tool also supports a set of self-explanatory manual migration commands:

  • isqlite add-column
  • isqlite alter-column
  • isqlite create-table
  • isqlite drop-column
  • isqlite drop-table
  • isqlite rename-column
  • isqlite rename-table
  • isqlite reorder-columns

Limitations

isqlite is highly suitable for applications that use SQLite as an application file format, and for ad hoc operations and migrations on existing SQLite databases. It is less suitable for circumstances in which traditional database engines are used (e.g., web applications), because if you eventually decide that you need to migrate from SQLite to a full-scale RDMS like MySQL or Postgres, you will have to rewrite all the code that uses isqlite.

Compared to SQLAlchemy

SQLAlchemy is a Python SQL toolkit and ORM and one of the most popular standalone SQL libraries for Python.

  • isqlite aims to be a replacement for Python's sqlite3 standard library, not a general-purpose database wrapper like SQLAlchemy. It does not support and will never support any database engine other than SQLite.
  • isqlite has a small and easy-to-understand API.
  • isqlite supports database migrations out of the box, while SQLAlchemy requires using an extension like Alembic.
  • isqlite is not an object relational mapper (ORM). It does not map database row to native Python objects. It just returns them as regular ordered dictionaries.
    • Note that SQLAlchemy includes an ORM but does not require that you use it.
  • isqlite comes with a command-line interface.

API documentation

API documentation is available at https://isqlite.readthedocs.io/en/latest/.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

isqlite-0.8.2.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

isqlite-0.8.2-py3-none-any.whl (35.7 kB view details)

Uploaded Python 3

File details

Details for the file isqlite-0.8.2.tar.gz.

File metadata

  • Download URL: isqlite-0.8.2.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for isqlite-0.8.2.tar.gz
Algorithm Hash digest
SHA256 e4809e646bdd9b1344f847cecbf832bc4528b0527db219df155ff2fdec51cd24
MD5 2acfb7c2b4db451a42becffc93388aa0
BLAKE2b-256 5229f3bb404bcd60f5d33c5966c168f78e867554910db81e35d591f2928fedd7

See more details on using hashes here.

File details

Details for the file isqlite-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: isqlite-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 35.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for isqlite-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5dd752ff229e4dc91cc0ac61595883103af65a367ca329b76114acba943d00d9
MD5 98f8c569aa85fd7356bae5f9d1adb462
BLAKE2b-256 34757a2ad119a35c46b9980d90253b0540309431e809965adf073092cd0e4b30

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page