Sqlite utilities for Python
Project description
wsqlite
wsqlite is a library for easy use of SQLite databases in Python.
Description
wsqlite simplifies code for working with SQLite databases in Python. It provides a set of classes and functions that make it easy to create, read, update, and delete data in SQLite databases.
Installation
To install the library, use pip:
pip install wsqlite
Description
The wsqlite library offers a number of general-purpose modules.
License
MIT
This project is licensed under the MIT License. See the LICENSE file for details.
Examples
This directory contains a collection of examples that demonstrate the usage of various modules and functionalities in this project. Each subfolder corresponds to a specific module and includes example scripts to help you understand how to use that module.
Directory Structure
The examples are organized as follows:
examples/
wsqlite/
crupd.py
new_columns.py
with_restrictions.py
How to Use
- Navigate to the module folder of interest, e.g.,
examples/module1/. - Open the
README.mdin that folder to get detailed information about the examples. - Run the scripts directly using:
python example1.py
Modules and Examples
wsqlite
Description
This module demonstrates specific functionalities.
- crupd.py: Example demonstrating functionality.
from pydantic import BaseModel, Field
from wsqlite import WSQLite
# Definir el modelo sin restricciones de email
class SimpleModel2(BaseModel):
id: int
name: str
age: int
is_active: bool
# Crear la base de datos y la tabla si no existe
db = WSQLite(SimpleModel2, "database.db")
# Insertar datos
db.insert(SimpleModel2(id=1, name="Juan Pérez", age=30, is_active=True))
db.insert(SimpleModel2(id=2, name="Ana López", age=25, is_active=True))
db.insert(SimpleModel2(id=3, name="Pedro Gómez", age=40, is_active=False))
# Buscar todos los registros
print("Todos los usuarios:", db.get_all())
# Buscar por un campo específico
print("Usuarios llamados Juan:", db.get_by_field(name="Juan Pérez"))
# Buscar por múltiples filtros
print("Usuarios activos con 25 años:", db.get_by_field(age=25, is_active=True))
# Actualizar un usuario
db.update(1, SimpleModel2(id=1, name="Juan Pérez", age=31, is_active=False))
print("Usuario actualizado:", db.get_by_field(id=1))
# Eliminar un usuario
db.delete(3)
print("Usuarios después de eliminar a Pedro:", db.get_all())
- new_columns.py: Example demonstrating functionality.
from pydantic import BaseModel, Field
from typing import Optional
from wsqlite import WSQLite
class SimpleModel4(BaseModel):
id: int = Field(..., description="Primary Key")
name: str = Field(..., description="NOT NULL")
age: int
is_active: bool
db = WSQLite(SimpleModel4, "database.db") # Esto creará la tabla
# Insertar un nuevo usuario con el nuevo campo
db.insert(SimpleModel4(id=1, name="Ana López", age=25, is_active=True))
# === AHORA AÑADIMOS UN NUEVO CAMPO AL MODELO ===
class SimpleModel4(BaseModel):
id: int = Field(..., description="Primary Key")
name: str = Field(..., description="NOT NULL")
age: int
is_active: bool
email: Optional[
str
] # nuevos campos no pueden tener restricciones en la base de datos (Field)
# Crear una nueva instancia con el modelo actualizado
db = WSQLite(SimpleModel4, "database.db") # Esto actualizará la tabla
# Insertar un nuevo usuario con el nuevo campo
db.insert(
SimpleModel4(
id=2, name="Ana López", age=25, is_active=True, email="ana@example.com"
)
)
# Mostrar los datos después de la actualización
print("Usuarios después de actualizar el modelo:", db.get_all())
- with_restrictions.py: Example demonstrating functionality.
from pydantic import BaseModel, Field
from typing import Optional
from wsqlite import WSQLite
# Definir el modelo con restricciones
class SimpleModel3(BaseModel):
id: int = Field(..., description="Primary Key")
name: str = Field(..., description="NOT NULL")
age: int
is_active: bool
email: Optional[str] = Field(None, description="UNIQUE") # Email debe ser único
# Crear la base de datos y sincronizar con el modelo
db = WSQLite(SimpleModel3, "database.db")
# Insertar datos válidos
db.insert(
SimpleModel3(
id=1, name="Juan Pérez", age=30, is_active=True, email="juan@example.com"
)
)
# Intentar insertar un usuario con el mismo email (esto fallará)
try:
db.insert(
SimpleModel3(
id=2, name="Ana López", age=25, is_active=True, email="juan@example.com"
)
)
except Exception as e:
print("Error al insertar usuario duplicado:", e)
# Insertar otro usuario con un email diferente (esto funcionará)
db.insert(
SimpleModel3(
id=3, name="Pedro Gómez", age=40, is_active=False, email="pedro@example.com"
)
)
# Ver datos almacenados
print("Usuarios en la base de datos:", db.get_all())
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wsqlite-0.1.0.tar.gz.
File metadata
- Download URL: wsqlite-0.1.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46d05afd39f55f959bf81ca3111086c2bb1c5b9af8e84589002fdeba19204076
|
|
| MD5 |
d65df20c11f7302ea0da1e50d9d2e102
|
|
| BLAKE2b-256 |
8108c94b90aec6545f3be9c3a2357b599fec58d26edaae84960ba962aace65b8
|
File details
Details for the file wsqlite-0.1.0-py3-none-any.whl.
File metadata
- Download URL: wsqlite-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0ae3b2b0398df60d26bd6ff5fb2c27dc92e48a7562fc7ff7f158814eadf7eaa
|
|
| MD5 |
18aa8a55f04c8f6f5d0c40e3bf8a23e1
|
|
| BLAKE2b-256 |
6e7ba999f60b71f201139bfd55933f0e16c1c1572a8e82771b8376a9282c9709
|