Skip to main content

Sql30

sql30 is a zero weight ORM for sqlite3 in Python.

It is written using pure python constructs and has no dependency on any other module. There is no requirements.txt in the project in fact. Having zero dependency on any other module makes this package usable on variety of platforms including the ones which have limited or delayed support such as ESX.

Usage

SQL30 is incredibly simple to use.

All it requires is to define your database schema using a simple JSON. Then, pre-defined interface can be used to perform CRUD operations. As is true for any ORM, user doesn't need to know how the operations are translated back to SQL statements for sqlite3.

In the schema,

  • Database filename is provided using the key db_name.
  • Multiple tables can be defined under tables key in JSON.
  • Fields / Columns for each table are added using fields key in JSON. Field name and it's type is provided as key value pair again.
  • Primary Key for each table can be defined using key primary_key

Let's take an example.

Example

Let's say we have to store the reviews of a product in database and we chose sqlite3 for the purpose. Using SQL30, a sample schema can be defined as shown below.

# reviews.py
from sql30 import db

class Reviews(db.Model):
    TABLE = 'reviews'
    PKEY = 'rid'
    DB_SCHEMA = {
        'db_name': 'reviews.db',
        'tables': [
            {
                'name': TABLE,
                'fields': {
                    'rid': 'uuid',
                    'header': 'text',
                    'rating': 'int',
                    'desc': 'text'
                    },
                'primary_key': PKEY
            }]
        }
    VALIDATE_BEFORE_WRITE = True

Now, we can create instance of Review and that will handle CRUD operations to database for us.

>>> import os
>>> import reviews

# delete old database if you want to
>>> os.remove('reviews.db')

# Create ORM layer object instance.
>>> db = reviews.Reviews()

# With this, we can create/write records to db as following.
>>> db.write(tbl, rid=1, header='good thing', rating=5)
>>> db.write(tbl, rid=2, header='bad thing', rating=1, desc='what a disgusting thing')

# We can read back the records as shown below.
>>> tbl = 'reviews'
>>> results = db.read(tbl, rid=1)

# Querying over the records is possble as below.
>>> results = db.read(tbl, rating=3, rid=1)

# If we try to add another record with same primary key, it will error out.
>>> db.write(tbl, rid=1, header='good thing', rating=5)

# Updating the record is also possible by providing condition for records and updated values.
>>> where = {'rid': 2}
>>> db.update(tbl, condition=where, header='average item', rating=2)

# Deleteing the records is possble with any level of filtering.
>>> db.remove(tbl, rid=1)

Repository

https://github.com/gitvipin/sql30

Requirements

Python 3.4+

Share and enjoy!

Metadata

Release files for sql30 0.1.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 sql30 0.1.1
File Size Uploaded
sql30-0.1.1.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sql30 0.1.1
File Interpreter ABI Platform
sql30-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.0 kB

Release files / sql30-0.1.1.tar.gz

Download URL sql30-0.1.1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0ddd26c62e20665148593b788f9ed3d2cd469c48a7199e7f957cf7a1598eb4b0
BLAKE2b-256 checksum
How to use checksums
9040d353e25a5197e2471b309052d8f8054db3514c49f68af0ae4f5a133beff2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.3

Release files / sql30-0.1.1-py3-none-any.whl

Download URL sql30-0.1.1-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a20d6ee44d615bad6536b9d5699ab4a3e33823903c8ed9e61f985cc390dfaa5
BLAKE2b-256 checksum
How to use checksums
adab346e74a71b42b6e494ba6ecb2db515cc2521325635d66bd8f3d9e52c4943
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.3

Release history Release notifications | RSS feed

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

This release

0.1.1 This release

2 release files

0.1.0

2 release files

0.0.1

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