Skip to main content

tiny_sqlite_orm

A simple ORM (Object-Relational Mapping) library for interacting with SQLite databases in Python, allowing you to work with your data in an object-oriented manner. The library focuses on simplicity and ease of use for small projects.

Installation

You can install it by running:

pip install tiny_sqlite_orm

How to Use

Setting Up the Database Connection

First, create an instance of the Database class to connect to the SQLite database.

from tiny_sqlite_orm import Database

# Connect to the database (or create it if it doesn't exist)
db = Database('my_database.db')

Defining Models

Models are defined as subclasses of the Table class. Each field in the model is an instance of a Field class. Here's an example of how to create a simple model:

from tiny_sqlite_orm import Table, TextField, IntegerField

class User(Table):

    # Table fields
    name = TextField(unique=True)
    age = IntegerField()

Creating the Tables

After defining your models, you can create the tables in the database:

# Create the tables if they don't already exist
db.create_tables_if_not_exists([User])

Inserting Data

You can insert new records into the database by running:

# Create a new user
user = User.create(name="John", age=30)
print(user.name)  # Returns: John

Selecting Data

To query data from the database, you can use the select method:

# Fetch users with age greater than or equal to 15
users = User.objects.select(age__ge=15)

# Iterate over the results
for user in users:
    print(user.name, user.age)

# Access the first and last user
first_user = users.first()
last_user = users.last()

See more about using select filters.

Updating Records

To update a record, you can:

  • Modify the object's attributes and call the save method:
user = User.objects.select(name="John").first()
if user:
    user.age = 31
    user.save()
  • Use the Table.objects.update() method:
User.objects.update(
    # Updates the age to 31
    fields={'age': 31},
    # Where name is "John"
    name="John"
)

Deleting Records

You can delete a record by calling the delete method on the object:

# Delete a user
user = User.objects.select(name="John").first()
if user:
    user.delete()

Or you can delete records using the delete method:

# Delete all users with the name John
User.objects.delete(name="John")

Using ForeignKey

You can define foreign key relationships between models. Here's an example with a Post model referencing a User:

from tiny_sqlite_orm import ForeignKeyField

class Post(Table):
    title = TextField()
    author = ForeignKeyField(User)

# Create the Post table
db.create_tables_if_not_exists([Post])

# Create a post related to a user
Post.create(title="My first post", author=user)

Aggregation Support

The library supports aggregation operations such as count, sum, avg, max, and min:

# Count the number of users
total_users = User.objects.count()

# Get the average age of users
average_age = User.objects.avg('age')

print(f'Total users: {total_users}')
print(f'Average age: {average_age}')

Using Select Filters

This method supports a variety of filters using __ (double underscore) syntax to specify conditions. Here are some common operators you can use:

  • No filters: Checks for equality (e.g., field=value).
  • field__ne: Checks for inequality (e.g., field__ne=value).
  • field__gt: Checks if the field is greater than a value (e.g., field__gt=value).
  • field__ge: Checks if the field is greater than or equal to a value (e.g., field__ge=value).
  • field__lt: Checks if the field is less than a value (e.g., field__lt=value).
  • field__le: Checks if the field is less than or equal to a value (e.g., field__le=value).
  • field__in: Checks if the field is in one of the values passed (e.g., field__in=[1, 2, 'test']).

The two below only work for string fields:

  • field__contains: Checks if the field's value contains the value (case sensitive) (e.g., field__contains="a").
  • field__icontains: Also checks if the field's value contains the value, but is case insensitive.

Here’s an example of how to use these operators in a query:

# Fetch users with age greater than or equal to 15
users = User.objects.select(age__ge=15)

# Fetch users whose name starts with exactly 'Jo'
users_with_Jo = User.objects.select(name__contains='Jo')

# Fetch users whose age is either 25 or 30
users_25_or_30 = User.objects.select(age__in=[25, 30])

# Iterate over the results
for user in users:
    print(user.name, user.age)

# Access the first and last user
first_user = users.first()
last_user = users.last()

Testing

To run tests, run:

python run_tests.py

Contributions

Contributions are welcome! Feel free to open a pull request or suggest improvements.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Metadata

Release files for tiny-sqlite-orm 1.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tiny-sqlite-orm 1.2.3
File Size Uploaded Provenance Actions
tiny_sqlite_orm-1.2.3.tar.gz 13.0 kB Without provenance Details for tiny_sqlite_orm-1.2.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tiny-sqlite-orm 1.2.3
File Interpreter ABI Platform Actions
tiny_sqlite_orm-1.2.3-py3-none-any.whl Python 3 none any Details for tiny_sqlite_orm-1.2.3-py3-none-any.whl

Total release size: 27.5 kB

Release files / tiny_sqlite_orm-1.2.3.tar.gz

Download URL tiny_sqlite_orm-1.2.3.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c6bc4752e8d3f32ef80a034f896737324c3361fa100747cefa895a577ede8249
BLAKE2b-256 checksum
How to use checksums
793cc67f0052533b390056b3d58cb09f2d661bea5c795d7a38eb75dc0af13b35
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.6

Release files / tiny_sqlite_orm-1.2.3-py3-none-any.whl

Download URL tiny_sqlite_orm-1.2.3-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea4dff6ad29f665b65f4e6328775a4d4d3e767f6242cfae6e1802c668aea0e3e
BLAKE2b-256 checksum
How to use checksums
a5ef9c13753f6dd80396da2ba0334ab2443c48ae1a4f7e47791d8c645bb11237
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.6

Security for tiny-sqlite-orm 1.2.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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