A brief description of your package
Project description
sopenqlite
- Automatic Resource Management: Automatically closes SQLite database connections and cursors, ensuring that resources are managed efficiently.
- Table Creation: Automatically creates necessary tables if they are missing.
- Pragma Commands: Easily executes one or multiple pragma commands to configure your SQLite database settings as needed.
See from the example below how the query function can be used.
import os
import sqlite3
from datetime import UTC, datetime
from functools import partial
from uuid import UUID, uuid4
from realerikrani.sopenqlite import query
DATABASE_PATH = os.environ.get("TODO_DATABASE_PATH", "todo.db")
CREATE_TABLES = """
CREATE TABLE IF NOT EXISTS todo (
id TEXT PRIMARY KEY,
task TEXT NOT NULL,
created_at TIMESTAMP NOT NULL
);
"""
_query = partial(query, CREATE_TABLES, DATABASE_PATH, ["PRAGMA journal_mode=WAL"])
def to_todo(row: sqlite3.Row | None) -> dict | None:
if row is None:
return None
return {
"id": UUID(row["id"]),
"task": row["task"],
"created_at": datetime.fromisoformat(row["created_at"]),
}
def create_todo(task: str) -> dict | None:
todo_id = str(uuid4())
created_at = datetime.now(UTC).isoformat()
q = "INSERT INTO todo (id, task, created_at) VALUES (?, ?, ?) RETURNING *"
return to_todo(
_query(lambda c: c.execute(q, (todo_id, task, created_at)).fetchone())
)
def read_todos() -> list[dict]:
return [
to_todo(row)
for row in _query(lambda c: c.execute("SELECT * FROM todo").fetchall())
]
def delete_todo(todo_id: UUID) -> dict | None:
q = "DELETE FROM todo WHERE id=? RETURNING *"
return to_todo(_query(lambda c: c.execute(q, (str(todo_id),)).fetchone()))
def main() -> None:
commands = {
"create": lambda: create_todo(input("Enter task: ")),
"read": lambda: read_todos(),
"delete": lambda: delete_todo(UUID(input("Enter TODO ID: "))),
}
while True:
command = input("Enter command (create/read/delete/exit): ").strip().lower()
if command in commands:
result = commands[command]()
if command == "read":
for todo in result:
print(f"{todo['id']} - {todo['task']}")
else:
print(f"{command.capitalize()}d TODO: {result}")
elif command == "exit":
break
else:
print("Unknown command.")
if __name__ == "__main__":
main()
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 realerikrani_sopenqlite-1.0.0.tar.gz.
File metadata
- Download URL: realerikrani_sopenqlite-1.0.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2827d6f47372c2463d5bd845efb4ce73a26f2f65f5b909e18f6b0a2df3a58e4
|
|
| MD5 |
018b949440ec2ed3c83e05d9c1c570b8
|
|
| BLAKE2b-256 |
e5f430f325187e0a946350c4d6c2c524008e9b83da4e521d38d5c0d093e5ddc5
|
File details
Details for the file realerikrani_sopenqlite-1.0.0-py3-none-any.whl.
File metadata
- Download URL: realerikrani_sopenqlite-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba75ff0a9a1894165ff6717f0f16a62569db7a8b494a0877af5ecce741e995ce
|
|
| MD5 |
242af2adf4993b5b074be2ad0c3c0430
|
|
| BLAKE2b-256 |
57683adc748907472876e13a62c7d8aa506724f0c1a2a90505eb515068b1fb4a
|