Skip to main content

Basic Orm for sqlite

Project description

SimpleORM

SimpleORM is a lightweight and easy-to-use Object-Relational Mapping (ORM) utility for Python built on top of SQLite. It provides basic CRUD (Create, Read, Update, Delete) operations by mapping Python classes directly to SQLite tables, simplifying database interactions without requiring complex setup.

Features

  • Automatically creates SQLite tables based on Python class annotations.
  • Insert Python objects into the database.
  • Retrieve all or single records as Python objects.
  • Delete records by object instance or ID.
  • Supports basic Python types: int, str, float.

Installation

No external dependencies are required besides Python's built-in sqlite3 module. Just include the SimpleORM class in your project.

Usage

  1. Define your data model as a Python class with type annotations.
class User:
    id: int
    name: str
    age: int

or

@dataclass
class User:
    id: int = field(init=False)
    name: str
    age: int
  1. Create a SimpleORM instance with the SQLite database filename:
from simple_orm import SimpleORM

orm = SimpleORM("mydatabase.db")
  1. Create the corresponding table for your model:
orm.create_table(User)
  1. Insert objects:
user = User()
user.name = "Alice"
user.age = 25
orm.insert(user)
print(user.id)  # Auto-generated ID

if usage dataclasses

user = User(name='Alice', age=25)
# ID is auto-generated
  1. Query all records:
users = orm.get_all(User)
for u in users:
    print(u.id, u.name, u.age)
  1. Query a single record by ID:
user = orm.get_one(User, 1)
if user:
    print(user.name, user.age)

if you used dataclasses, make only

if user:
    print(user)

because dataclasses add repr 7. Delete records:

orm.delete(user)            # Delete by object
orm.delete_by_id(User, 2)  # Delete by ID
orm.delete_all(User)       # Delete all records for the class

Limitations

  • Supports only simple types (int, str, float).
  • Does not support complex queries, relationships, or migrations.
  • Assumes classes have an id attribute for primary keys.

License

This project is released under the MIT License.

how to contribute?

create a PR on github

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

simple_orm_sqlite-0.0.3.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

simple_orm_sqlite-0.0.3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file simple_orm_sqlite-0.0.3.tar.gz.

File metadata

  • Download URL: simple_orm_sqlite-0.0.3.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for simple_orm_sqlite-0.0.3.tar.gz
Algorithm Hash digest
SHA256 df454944e7a8cc8df5f6c6e919dcf0bf1152c7ff9a84076f38aadfc0ee1334a1
MD5 1b6fc583e99fcae322f02a74fb5edc60
BLAKE2b-256 455e9a7702aee4dad12b5fd0bb0bd70426e9bb7278fdebf785c9ff59a8205e74

See more details on using hashes here.

File details

Details for the file simple_orm_sqlite-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_orm_sqlite-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5200c8a9f904a23b92b73308d4ec2cfe5f96565c5648538af9bf53f419e31ddb
MD5 6b283a915903401043abb44511e3346c
BLAKE2b-256 b5dc4c46a33a9622127c91e367defae382eff164af576023578b3a5b4e522c62

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