A simple ORM for SQLite
Project description
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.
-
Define a
Postmodel in apost.pyfile.# post.py from orm import Model class Post(Model): text = str # other datatypes: int, float def __init__(self, text): self.text = text
-
Import
Databaseto create a data access object.>>> from orm import Database >>> db = Database('db.sqlite') # indicating a database file.
-
Import the
Postmodel and link it to the database.>>> from post import Post >>> Post.db = db # see another approach in tests.py
-
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
-
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
-
Commit all staged operations (
saveandupdate) to the database.>>> db.commit()
-
Delete the object and commit.
>>> post.delete() >>> db.commit()
-
Create a manager that can perform CRUD operations in the database.
>>> objects = Post.manager(db)
-
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'}
-
Close the database without commit the changes
>>> db.close()
-
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
The MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple-orm-0.0.1.tar.gz.
File metadata
- Download URL: simple-orm-0.0.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
742b4ecc06c46d45a02f67cf1fac8ff85ae26946df17e449b1a9068396699a00
|
|
| MD5 |
6d3bd64e727fa3ebfb30c6011f3e73c5
|
|
| BLAKE2b-256 |
34bf8ee9357b14f1e4b5a46cdd1c3ce11890b5e1fccabfd88cb12aa74c6febfd
|
File details
Details for the file simple_orm-0.0.1-py3-none-any.whl.
File metadata
- Download URL: simple_orm-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1262eafb193c0483d9d2d9462004453e0d797b17cdc42b2b3e5c83fd6edb5a3
|
|
| MD5 |
d276aa0dbc7c62f40491c9cf31f52728
|
|
| BLAKE2b-256 |
4dda04eb2e20bf707658ba57da91d47ed449249e4cfd2fb2609ea41c4ce8aa89
|