Skip to main content

SQLite simple ORM

It is a simple and small ORM for SQLite database. An object-relational mapper (ORM) is a code library that automates the transfer of data stored in relational databases tables into objects that are more commonly used in application code.

  • python 3+ (developed with 3.7)
  • supports SQLite only

Install

pip install py-sqlite-orm-danidr

Usage

from sqlite_orm.database import Database
from sqlite_orm.field import IntegerField, BooleanField, TextField
from sqlite_orm.table import BaseTable

import logging


class User(BaseTable):
    __table_name__ = 'users'

    id = IntegerField(primary_key=True, auto_increment=True) #autoincrement int field
    name = TextField(not_null=True)
    active = BooleanField(not_null=True, default_value=1)


class Post(BaseTable):
    __table_name__ = 'posts'

    id = IntegerField(primary_key=True)
    name = TextField(not_null=True)
    id_user = IntegerField(foreign_key=User.id)


if __name__ == '__main__':

    #logger configure:
    logging.basicConfig(filename="sample.log", level=logging.DEBUG, format=('%(asctime)s: '
                                                                            '%(filename)s: '
                                                                            '%(levelname)s: '
                                                                            '%(funcName)s(): '
                                                                            '%(lineno)d: '
                                                                            '%(message)s'), datefmt="%Y-%m-%d %H:%M:%S")
    with Database("test.db") as db:
        # create table
        db.query(Post, User).create().execute()

        user1 = User(id=1, name='User1')
        user2 = User(id=2, name='User2')
        user3 = User(id=3, name='User3')

        post1 = Post(id=1, name='Post1', id_user=user1.id)
        post2 = Post(id=2, name='Post2', id_user=user2.id)
        post3 = Post(id=3, name='Post3', id_user=user3.id)

        #insert data
        db.query().insert(user1, user2, user3, post1, post2, post3).execute()

        # select with columns + autojoin with fk;
        print('\n=======SELECT + Auto Join=======')
        for row in db.query(User, Post.name).select().join(Post).execute():
            print(row)

        # update
        db.query(User).update(name='User3_UPDATED').filter(User.name == 'User3').execute()

        print('\n=======SELECT after update=======')
        for row in db.query(User, Post.name).select().join(Post).execute():
            print(row)

        db.query(User).delete().filter(User.name == 'User3_UPDATED').execute()

        print('\n=======SELECT after delete=======')
        for row in db.query(User, Post.name).select().join(Post).execute():
            print(row)

        # delete
        db.query(User, Post).drop().execute()

Release files for py-sqlite-orm-danidr 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 py-sqlite-orm-danidr 0.0.1
File Size Uploaded
py-sqlite-orm-danidr-0.0.1.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-sqlite-orm-danidr 0.0.1
File Interpreter ABI Platform
py_sqlite_orm_danidr-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.1 kB

Release files / py-sqlite-orm-danidr-0.0.1.tar.gz

Download URL py-sqlite-orm-danidr-0.0.1.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3e131d87225f8e132919dea113812a3e26ced7bf02789045dc7a708b94db1f0c
BLAKE2b-256 checksum
How to use checksums
bfa2a73c485a551e9010706bbaa3db3f8b9d2e19e5ba01ee39ff02f8e28eed04
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

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

Download URL py_sqlite_orm_danidr-0.0.1-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a243cc383800f29572f3326e5aa8352f5c964b39343ae317ed4104dff8ee17e9
BLAKE2b-256 checksum
How to use checksums
098e0ffadd614832c7411c83ee681660a20bef075059c01696ca43566fe6391d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

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