Skip to main content

python-sqlite-orm

A Python object relational mapper for SQLite.

Install

pip install simple-orm

Usage

Following a basic tutorial to demonstrate how to use the ORM.

  1. Define a Post model in a post.py file.

    # post.py
    from orm import Model
    
    class Post(Model):
    
        text = str  # other datatypes: int, float
    
        def __init__(self, text):
            self.text = text
    
  2. Import Database to create a data access object.

    >>> from orm import Database
    >>> db = Database('db.sqlite')  # indicating a database file.
    
  3. Import the Post model and link it to the database.

    >>> from post import Post
    >>> Post.db = db  # see another approach in tests.py
    
  4. Create a post and save it in the staging area (without commit) of database.

    >>> post = Post('Hello World').save()
    >>> print(post.id)  # auto generated id
    1
    
  5. Change the hello world post and update it in the database.

    >>> post = Post.manager().get(id=1)
    >>> post.text = 'Hello Mundo'
    >>> post.update()
    >>> post.text
    Hello Mundo
    
  6. Commit all staged operations (save and update) to the database.

    >>> db.commit()
    
  7. Delete the object and commit.

    >>> post.delete()
    >>> db.commit()
    
  8. Create a manager that can perform CRUD operations in the database.

    >>> objects = Post.manager(db)
    
  9. Save and get a post.

    >>> objects.save(Post('Hello', 'World'))
    >>> objects.get(2)  # get by id from the staging area
    {'text': 'World', 'id': 2, 'title': 'Hello'}
    
  10. Close the database without commit the changes

    >>> db.close()
    
  11. Get all posts from database.

    >>> list(objects.all())  # return a "empty" generator
    []
    

Linter

Check code lint:

pip install pylint
pylint orm.py

Contributing

See CONTRIBUTING.

License

CC0

The MIT License.

Release files for simple-orm 0.0.1

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

Source distribution (sdist)

Source distribution for simple-orm 0.0.1
File Size Uploaded
simple-orm-0.0.1.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for simple-orm 0.0.1
File Interpreter ABI Platform
simple_orm-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.8 kB

Release files / simple-orm-0.0.1.tar.gz

Download URL simple-orm-0.0.1.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
742b4ecc06c46d45a02f67cf1fac8ff85ae26946df17e449b1a9068396699a00
BLAKE2b-256 checksum
How to use checksums
34bf8ee9357b14f1e4b5a46cdd1c3ce11890b5e1fccabfd88cb12aa74c6febfd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

Release files / simple_orm-0.0.1-py3-none-any.whl

Download URL simple_orm-0.0.1-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c1262eafb193c0483d9d2d9462004453e0d797b17cdc42b2b3e5c83fd6edb5a3
BLAKE2b-256 checksum
How to use checksums
4dda04eb2e20bf707658ba57da91d47ed449249e4cfd2fb2609ea41c4ce8aa89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

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