A Python object relational mapper for SQLite.
Project description
# python-sqlite-orm
A Python object relational mapper for SQLite.
## Tutorial
* Install
```sh
pip install sqlite-orm
```
* Define a Post model in a `post.py` file.
```py
# post.py
from orm import Model
class Post(Model):
text = str # other datatypes: int, float
def __init__(self, text):
self.text = text
```
* Import `Database` to create a data access object (DAO).
```py
>>> from orm import Database
>>> db = Database('db.sqlite') # indicating a database file.
```
* Import the Post model and link it to the database.
```py
>>> 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.
```py
>>> post = Post('Hello World').save()
>>> print(post.id) # auto generated id
1
```
* Change the hello world post and update it in the database.
```py
>>> post.text = 'Hello Mundo'
>>> post.update()
>>> post.text
Hello Mundo
```
* Commit all staged operations (`save` and `update`) to the database.
```py
>>> db.commit()
```
* Delete the object and commit.
```py
>>> post.delete()
>>> db.commit()
```
* Create a manager that can perform CRUD operations in the database.
```py
>>> objects = Post.manager(db)
```
Save and get a post.
```py
>>> 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
```py
>>> db.close()
```
Get all posts from database.
```py
>>> list(objects.all()) # return a "empty" generator
[]
```
## Contributing
See CONTRIBUTING.
## License
[![CC0](https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png)](https://creativecommons.org/licenses/by-nc-sa/4.0/)
The MIT License.
-
Copyright (c) 2014-2016 [Fernando Felix do Nascimento Junior](https://github.com/fernandojunior/).
A Python object relational mapper for SQLite.
## Tutorial
* Install
```sh
pip install sqlite-orm
```
* Define a Post model in a `post.py` file.
```py
# post.py
from orm import Model
class Post(Model):
text = str # other datatypes: int, float
def __init__(self, text):
self.text = text
```
* Import `Database` to create a data access object (DAO).
```py
>>> from orm import Database
>>> db = Database('db.sqlite') # indicating a database file.
```
* Import the Post model and link it to the database.
```py
>>> 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.
```py
>>> post = Post('Hello World').save()
>>> print(post.id) # auto generated id
1
```
* Change the hello world post and update it in the database.
```py
>>> post.text = 'Hello Mundo'
>>> post.update()
>>> post.text
Hello Mundo
```
* Commit all staged operations (`save` and `update`) to the database.
```py
>>> db.commit()
```
* Delete the object and commit.
```py
>>> post.delete()
>>> db.commit()
```
* Create a manager that can perform CRUD operations in the database.
```py
>>> objects = Post.manager(db)
```
Save and get a post.
```py
>>> 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
```py
>>> db.close()
```
Get all posts from database.
```py
>>> list(objects.all()) # return a "empty" generator
[]
```
## Contributing
See CONTRIBUTING.
## License
[![CC0](https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png)](https://creativecommons.org/licenses/by-nc-sa/4.0/)
The MIT License.
-
Copyright (c) 2014-2016 [Fernando Felix do Nascimento Junior](https://github.com/fernandojunior/).
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
sqlite-orm-0.0.1b0.tar.gz
(4.1 kB
view hashes)
Built Distribution
Close
Hashes for sqlite_orm-0.0.1b0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 301620f470a132bc834a932b0b8c7ddb1068327a7e161041b9034a4adf56c1a3 |
|
MD5 | 59d94d31da2ab0fde7c377fabbb715c7 |
|
BLAKE2b-256 | 0d8113c2375c5f9b583ab2a6d63b22ea9bae7a08cdbed43055354dcfb4216960 |