Skip to main content

An improved Python interface to SQLite

Project description

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

WARNING: isqlite is in beta. Not all features described here have been implemented yet. If you want to try it out, back up your data first.

from isqlite import Database, Table, columns
from isqlite import query as q

# Note that in addition to the columns declared explicitly, isqlite will automatically
# create `id`, `created_at` and `last_updated_at` columns.
teams_table = Table("teams", [
  columns.Text("name", required=True),
])
employees_table = Table("employees", [
  columns.Text("name", required=True),
  columns.Integer("age", required=False),
  columns.ForeignKey("team", "teams", required=False),
])

with Database("db.sqlite3") as db:
    # Create the tables defined in the database. This only needs to be done once.
    db.create_table(teams_table)
    db.create_table(employees_table)

    # 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("employees", pk)
    print(person["name"], person["age"])

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

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

    # Filter rows with a query.
    employees = db.list("employees", q.Like("name", "John%") and q.GreaterThan("age", 40))

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

Features

  • A more convenient 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 simplify common patterns, e.g. get_or_create.
  • Automated database migrations: adding, removing, altering and reordering columns.
  • Support for decimal.Decimal, datetime.time and bool database columns.
  • A command-line interface.

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

TODO

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.2.tar.gz (10.9 kB view details)

Uploaded Source

Built Distributions

isqlite-0.2.0-py3-none-any.whl (47.5 kB view details)

Uploaded Python 3

isqlite-0.2-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: isqlite-0.2.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.9

File hashes

Hashes for isqlite-0.2.tar.gz
Algorithm Hash digest
SHA256 22d0d54fe0527342cde36fc37728c20e732b9bc4e62eeec6ebe2f71a4892e8f7
MD5 f20919459c01e294cc3059f67abad297
BLAKE2b-256 c05a087df5e453a0d71204d3bcb617733e11af2748ca36d19135535e7d3834f4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: isqlite-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 47.5 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 368f855ed27e6f9ce1ff40ffbe9e2ac83a790d7daf2ba7cdfb141748371b0df6
MD5 65e2d1b1fa7c8a31f9e6b0190605ab0f
BLAKE2b-256 132404d11989ae205d702dd5195aa000247b37c7069752064fcd870b9877f982

See more details on using hashes here.

File details

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

File metadata

  • Download URL: isqlite-0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.9

File hashes

Hashes for isqlite-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 37d8a8a21f1216846632949f84bc35fa7aeafd1e71d7777d64d52bdb6fdd2295
MD5 ecdbc8df110e9a1a6253f7f97ae6e7dd
BLAKE2b-256 d4d370fddaaa04ab1e750e117be6ca8a382374b7eb18fe819dc50ae00133d12d

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