Catalog PyPI
PACKAGE EXPLORERView in registry

af-db-teach

PyPI

Core SQLite ORM with separate educational modules

Versions collected1
Direct dependencies0From release 0.1.0
Last observed20 Oct, 21:01 UTC

README

Version 0.1.0 documentation
# 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

MethodDescription
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.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 1

Collected releases
VersionPublishedDirect dependenciesOpen version
0.1.010 Jun 20260

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
abbasfaramarzi/af_db_teach

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 21:01 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:603d28fbb71222341b56b0d0dffe0cc9d19fda2768a387fa55df54523dc564c7