A simple ORM for SQLite databases in Python
Project description
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
To use the library, simply copy the files to your project. There are no external dependencies aside from SQLite, which comes with the standard Python installation.
How to use
Setting up the database connection
First, you need to create an instance of the Database
class to connect to the SQLite database.
from sqlite_orm.database 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 sqlite_orm.table import Table
from sqlite_orm.field import TextField, IntegerField
class User(Table):
# Bind the database to the model within the class
db = db
# 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)
user.name
# Returns: John
Selecting data
To query data from the database, you can use the select
method:
# Fetch users with age greater or
# equal (__ge) than 15
users = User.objects.select(
age__ge=15
)
# Iterate over the results
for user in users:
print(user.name, user.age)
users.first()
users.last()
Updating records
To update a record, you can:
- modify the object's attributes and call the
save
method again:
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="Jonh"
)
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 using the delete method:
# Delete all users with
# name John
User.objects.delete(name="John")
Using ForeignKey
You can also define foreign key relationships between models. Here's an example with a Post
model referencing a User
:
from sqlite_orm.field import ForeignKeyField
class Post(Table):
# Bind the database within the class
db = db
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)
Aggregations 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}')
Contributions
Contributions are welcome! Feel free to open a PR or suggest improvements.
License
This project is licensed under the MIT License. See the LICENSE file for details.
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
Built Distribution
File details
Details for the file tiny_sqlite_orm-0.1.0.tar.gz
.
File metadata
- Download URL: tiny_sqlite_orm-0.1.0.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a87221484531458494eee9d4f8b81217285d09cef090c2f75dcede66ce36e58 |
|
MD5 | 05a9b1024f5bcc6d87d098bfe474752c |
|
BLAKE2b-256 | 69bdf63052c617061880db6caf0198ba85b67bc1b42d0392ad16b2b2cd393393 |
File details
Details for the file tiny_sqlite_orm-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: tiny_sqlite_orm-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 905d4b38db180ccf0d0f58ed3c610481fa30d382d0e215a3fddade5b5abdbd5e |
|
MD5 | e366d0c5d291e15821e7bb23608e8000 |
|
BLAKE2b-256 | 5d3bd03a9c084c8a0c5aec3b47edd9ade59e04a472524dde16f7038a33e32e5f |