Skip to main content

Simple SQLite3 ORM

Project description

Simple SQLite ORM - 'sORM'

Allows to use Python objects instead of SQLite statements.

Installation

You can install 'sORM' with these commands:

    $ mkdir <installation dir>
    $ cd /<installation dir>
    $ git clone git://github.com/max-kim/sorm.git
    $ cd sorm

Requirements

  • Python 3.6 (or over)

Usage

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)
    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(Group.id).all()

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.id, 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'),
                                             (User.id, '=', 2)).value(group_id=admin_group.id)
        print(user)

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

Deletion the data:

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

or:

    some_user = User(id=5)
    connection.delete(some_user)

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

The deletion statement returns 'None' any time.

Other

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.

Source Distribution

sorm-0.0.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sorm-0.0.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file sorm-0.0.1.tar.gz.

File metadata

  • Download URL: sorm-0.0.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.5

File hashes

Hashes for sorm-0.0.1.tar.gz
Algorithm Hash digest
SHA256 7f060fa7af42e4c3e1b724dbf6defdc8ff8e8a25f059f74ac56a1f900ef6749c
MD5 e527e901af2bf9e0dc0303a9b9bd98a8
BLAKE2b-256 ca98435f733369d4a6d22b40292f6497e459ff68c0d1973c0c74520b7a571574

See more details on using hashes here.

File details

Details for the file sorm-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: sorm-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.5

File hashes

Hashes for sorm-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1e3ed5e46c5b3ce03ca2eedc9ac7fe886026774d2aac140835da05d3e2beef89
MD5 234be978585faae183acd84ef7b0b5b9
BLAKE2b-256 dde84852947d967fdaf11dc61a1a652c7f947572c2d2b2c7e99d76dfa3d2e646

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page