Skip to main content

Simple SQLite3 ORM

Project description

Simple SQLite ORM - 'sORM'

Allows to use Python objects instead of SQLite statements.


You can install 'sORM' with these commands:

    $ mkdir <installation dir>
    $ cd /<installation dir>
    $ git clone git://
    $ cd sorm


  • Python 3.6 (or over)


How to make the connection and create the tables:

    from sorm import create_connection
    from sorm import IntType, FloatType, StrType, BytesType, ForeignKey, Base, Relationship
    from sorm import Base, ForeignKey, Relationship

    connection = create_connection('sorm_test.db', echo=True)

    class Group(Base):
        __tablename__ = 'groups'

        id = IntType(__tablename__, 'id', primary_key=True)
        group_name = StrType(__tablename__, 'group_name', nullable=False)

    class User(Base):
        __tablename__ = 'users'

        id = IntType(__tablename__, 'id', primary_key=True)
        user_name = StrType(__tablename__, 'user_name', nullable=False)
        group_id = IntType(__tablename__, 'group_id', ForeignKey(Group, 'id'))

        group = Relationship(group_id)

    connection = create_connection('sorm_test.db', echo=True)
    connection.create_table(Group, User)

    # Also you can use next syntax to create table:
    # <<< Group.create(connection)

Important: For the best way, use 'id' attribute as primary key within your every tables.

Use 'echo=True' as 'create_connection' parameter to show every sql queries.

How to insert data:

    group = Group(group_name='Admins')
    connection.add(Group(**{'group_name': 'Users'}))

    connection.add(User(**{'user_name': 'Max', 'group_id': None}))
    connection.add(User(**{'user_name': 'Alex', 'group_id': None}))

The field 'id' will be added and filled automatically.

How to select data:

    groups = connection.query(Group).order_by(

The result returns as tuple of objects:

    print(type(group)) # >>> <class 'tuple'>
    for group in groups:
        print('The group id = {}, group name is {}.'.format(, group.group_name))

When you call 'first()' method, you get an object or 'None' if the query does not match any data.

    admin_group = connection.query(Group).where((Group.group_name, '=', 'Admins')).first()

How to make an update:

    if admin_group:
        user = connection.update(User).where((User.user_name, '=', 'Alex'),
                                             (, '=', 2)).value(

Update query returns tuple of objects any time or empty tuple if conditions does not fit anything.

Deletion the data:

    connection.delete(Group).where((, '>=', 3))


    some_user = User(id=5)

    # connection.delete(some_user) == connection.delete(User).where((, '=', 5))

The deletion statement returns 'None' any time.


This library is a homewokr for OTUS Python web-dev.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for sorm, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size sorm-0.0.1-py3-none-any.whl (10.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size sorm-0.0.1.tar.gz (8.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page