Skip to main content

A handy way to interact with an SQLite database from Python

Project description

Litequery

Litequery is a minimalist library for interacting with SQLite in Python. It lets you define your queries once and call them as methods. No ORM bloat, just raw SQL power, with the flexibility to operate in both asynchronous and synchronous modes.

Why Litequery?

  • Simplicity: Define SQL queries in .sql files. No complex ORM layers.
  • Async first: Built for modern async Python, but also supports synchronous operations for traditional use cases.
  • Flexible: Supports different SQL operations seamlessly.

Installation

pip install litequery

Getting Started

Define Your Queries

Create a queries.sql file. Name your queries using comments and write them in pure SQL.

-- name: get_all_users
SELECT * FROM users;

-- name: get_user_by_id^
SELECT * FROM users WHERE id = :id;

-- name: get_last_user_id$
SELECT MAX(id) FROM users;

-- name: insert_user<!
INSERT INTO users (name, email) VALUES (:name, :email);

-- name: delete_all_users!
DELETE FROM users;

Using Your Queries

Define your database and queries, and then call them as methods. Choose async or sync setup based on your needs. It's as straightforward as it sounds.

import litequery
import asyncio


async def main():
    lq = litequery.setup("database.db", "queries.sql", use_async=True)
    await lq.connect()

    user_id = await lq.insert_user(name="Alice", email="alice@example.com")
    print(user_id)

    users = await lq.get_all_users()
    print(users)

    user = await lq.get_user_by_id(id=user_id)
    print(user)

    rows_count = await lq.delete_all_users()

    await lq.disconnect()


asyncio.run(main())

Transaction Support

Litequery also supports transactions in both async and sync contexts, allowing you to execute multiple queries atomicaly.

import litequery
import asyncio


async def main():
    lq = litequery.setup("database.db", "queries.sql")
    await lq.connect()

    try:
        async with lq.transaction():
            await lq.insert_user(name="Charlie", email="charlie@example.com")
            raise Exception("Force rollback")
            await lq.insert_user(name="Eve", email="eve@example.com")
    except Exception:
        print("Transaction failed")

    users = await lq.get_all_users()
    print(users)

    await lq.disconnect()


asyncio.run(main())

Wrapping Up

Litequery is all about simplicity and efficiency. Why wrestle with bloated ORMs when you can have raw SQL power? If you think there's a better way or have suggestions, let's hear them. Happy querying!

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

litequery-0.6.3.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

litequery-0.6.3-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file litequery-0.6.3.tar.gz.

File metadata

  • Download URL: litequery-0.6.3.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for litequery-0.6.3.tar.gz
Algorithm Hash digest
SHA256 97789d3e0cf19b22158a8abac6e657321a55eb4be1fdfd5f0dff6e92fda2cc6d
MD5 1adfeddc50d481a9b1b8c2171f90871a
BLAKE2b-256 b84a38a35a37033b3dbeb610b5760e0d5b4f52d7529a9382485146cc4226decd

See more details on using hashes here.

File details

Details for the file litequery-0.6.3-py3-none-any.whl.

File metadata

  • Download URL: litequery-0.6.3-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for litequery-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7ff6c339c651eb79ef385d3c5480c6ff127b890c46cbb3dc4d6741388bc010cb
MD5 ae4dd6b8624f938f88da6f6fe96deac6
BLAKE2b-256 ab11ef4a9f84a700a5e93a6a1f739232d4e40c525ba9a980bffb86a0d41e68dd

See more details on using hashes here.

Supported by

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