Skip to main content
# af_db_teach

A simple, educational ORM for SQLite in Python.  
This package allows you to work with an SQLite database without writing raw SQL queries.  
Designed for small projects and learning ORM concepts.

## Features

- Define models using Python classes and simple attributes
- Automatic CRUD operations (Create, Read, Update, Delete)
- Automatic table management (create, update schema, drop)
- Support for default data (`_default_data`)
- Helper methods like search, borrow/return (in the library example)
- Export all tables and data as a dictionary with `db_deeep_interface`

## Installation

```bash
pip install af_db_teach

Note: This package will be published on PyPI soon. You can also use it directly from the GitHub repository.

Quick Start

1. Define a Model

from af_db_teach import Model

class Product(Model):
    _primary_key = "id"
    id = 0
    name = ""
    price = 0.0
    in_stock = 1
  • The table name is automatically derived from the class name (lowercase): product
  • Fields are defined as class attributes.
  • Data types (INTEGER, REAL, TEXT) are inferred from the example values (int, float, str).

2. Work with the Database

# Connect and create the table
db = Product("shop.db")

# Insert a row
new_id = db.insert_one(name="Python Book", price=25000, in_stock=1)

# Get all records
all_products = db.get_all()

# Get a single record by primary key
product = db.get_one(1)

# Update a record
db.update(1, price=27000)

# Delete a record
db.delete(1)

# Drop the entire table
db.delete_table()

3. Default Data

You can define _default_data in your model class to insert initial records:

class Category(Model):
    id = 0
    title = ""

    _default_data = [
        (1, "Electronics"),
        (2, "Books"),
        (3, "Clothing"),
    ]

Note: The order of values must match the order of fields returned by table_fields().

Model Methods

Method Description
insert_one(**kwargs) Insert a new row. Returns lastrowid.
get_all(where="", params=()) Get all rows (optional WHERE clause).
get_one(row_id) Get a single row by primary key.
update(row_id, **kwargs) Update specified fields.
delete(row_id) Delete a row by primary key.
delete_table(table_name=None) Drop the entire table.
db_deeep_interface() Return a dictionary of all tables and their contents.

Complete Example: Library Management

The file library_orm_demo.py provides a practical library system example:

from af_db_teach import Model
from datetime import date

class Book(Model):
    _primary_key = "id"
    id = 0
    title = ""
    author = ""
    year = 2000
    is_available = 1

    def borrow(self, book_id):
        book = self.get_one(book_id)
        if book and book['is_available'] == 1:
            return self.update(book_id, is_available=0)
        return False

    def return_book(self, book_id):
        book = self.get_one(book_id)
        if book and book['is_available'] == 0:
            return self.update(book_id, is_available=1)
        return False

# Usage
book_db = Book("library.db")
book_db.insert_one(title="Thus Spoke Zarathustra", author="Nietzsche", year=1885)
book_db.borrow(1)       # borrow the book
book_db.return_book(1)  # return it

Run the full demo:

python library_orm_demo.py

Advanced Notes

1. Ignoring a Table

The Keywords class contains a list ignores_table_name_list. If a table name is in this list, create_table will not create it. You can extend this list:

from af_db_teach import Keywords
Keywords.ignores_table_name_list.append("my_temp_table")

2. Base Class TableBase

If you need a model without direct database connection (just the table structure), you can inherit from TableBase.

3. Connecting to an Existing Database

db = Model("my_database.db")

If the database file does not exist, it will be created.

License

This project is released under the MIT License.

Author

Abbas Faramarzi
Email
GitHub Repository


af_db_teach – A simple tool for learning and using ORM in small Python projects.

Metadata

Release files for af-db-teach 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for af-db-teach 0.1.0
File Size Uploaded Provenance Actions
af_db_teach-0.1.0.tar.gz 5.6 kB Without provenance Details for af_db_teach-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for af-db-teach 0.1.0
File Interpreter ABI Platform Actions
af_db_teach-0.1.0-py3-none-any.whl Python 3 none any Details for af_db_teach-0.1.0-py3-none-any.whl

Total release size: 11.8 kB

Release files / af_db_teach-0.1.0.tar.gz

Download URL af_db_teach-0.1.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
cd612f13905bf45a52d6969bb5722baa4711ab6866a12903658caa74ffbe10cd
BLAKE2b-256 checksum
How to use checksums
8d4bfb7c0f5906766b8aeb917e2d51600b17e7ff9bcbc0f2a0a3f8cb3a0bb461
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / af_db_teach-0.1.0-py3-none-any.whl

Download URL af_db_teach-0.1.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9c4536cd512c38e57b192c4a850c4a8151aee81de9d910e4ea345959adc2ce9b
BLAKE2b-256 checksum
How to use checksums
d1a92c2975c84f99299e2d0d1b70282accdbae673d5587caf6d055bc5a963fd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Security for af-db-teach 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page