simple DAO builder and abstraction for sqlite
Project description
SqliteDao
A simplified DAO for SQL abstraction for personal projects.
pip install sqlitedao
Demo project: sqlite_img_app
Examples
Create Table easily:
from sqlitedao import SqliteDao
dao = SqliteDao.get_instance(DB_PATH)
create_table_columns = {
"name": "text",
"position": "text",
"age": "integer",
"height": "text"
}
dao.create_table(TEST_TABLE_NAME, create_table_columns)
Or with a bit more control:
from sqlitedao import ColumnDict
columns = ColumnDict()
columns\
.add_column("name", "text", "PRIMARY KEY")\
.add_column("position", "text")\
.add_column("age", "integer")\
.add_column("height", "text")
create_table_indexes = {
"name_index": ["position"]
}
dao.create_table(TEST_TABLE_NAME, columns, create_table_indexes)
Retrieve items as a list of python dictionaries:
from sqlitedao import SearchDict
search = SearchDict().add_filter("age", 50, operator=">")
rows = xdao.search_table(TEST_TABLE_NAME, search)
# [{"name": "Michael Jordan", "position": "SG", "age": 56, "height": "6-6"}]
Create DAO classes by inheriting TableItem
easily and deal with less code:
from sqlitedao import TableItem, SearchDict
class Player(TableItem):
TABLE_NAME = TEST_TABLE_NAME
INDEX_KEYS = ["name"]
ALL_COLUMNS = {
"name": str,
"position": str,
"age": int,
"height": str
}
def __init__(self, row_tuple=None, **kwargs):
super().__init__(row_tuple, **kwargs)
self.load_tuple()
def load_tuple(self):
self.name = self.row_tuple["name"]
self.position = self.row_tuple["position"]
self.age = self.row_tuple["age"]
self.height = self.row_tuple["height"]
def grow(self):
self.age += 1
self.row_tuple['age'] += 1
# Perform DAO action with above structured class
dao.insert_item(item)
dao.insert_items(items)
dao.update_item(changed_item)
dao.update_items(changed_items)
dao.find_item(item_with_only_index_populated)
# Pagination with sqlite is easier than ever!
old = SearchDict().add_filter("age", 35, ">")
old_team_1 = dao.get_items_page(Player, old, None, limit = 10)
old_team_2 = dao.get_items_page(Player, old, old_team_1[-1], limit = 10)
see test files for more examples. This can greatly simplify and ease the creation cost for pet projects based on sqlite.
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
sqlitedao-0.7.1.tar.gz
(6.8 kB
view details)
Built Distribution
File details
Details for the file sqlitedao-0.7.1.tar.gz
.
File metadata
- Download URL: sqlitedao-0.7.1.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 33a6db656f68d0646f5faa4692e9665389a891e948e162650d61377ba8eedc07 |
|
MD5 | 8f9e9939d31705b0f6d3632f7005505f |
|
BLAKE2b-256 | 4ac2de29f821e0ae6b4189348531c47073c3989df85081f5e07a804f533fd8fb |
File details
Details for the file sqlitedao-0.7.1-py3-none-any.whl
.
File metadata
- Download URL: sqlitedao-0.7.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 415496d95a62038a66822dc04fe4283081035fe41614a3abd0b5cd397a817d66 |
|
MD5 | 73cfd95be28fa9050882b5c516e90c8a |
|
BLAKE2b-256 | 59f4d553e40949e2520f322094a4420be0dde02d201efd8f31ae989e7651d803 |