A lightweight, beginner-friendly SQLite wrapper for Python
Project description
minidblite ๐๏ธ
A lightweight, beginner-friendly SQLite wrapper for Python โ no SQL required.
What is minidblite?
minidblite lets you create and work with a local SQLite database using
clean, Pythonic method calls. No SQL knowledge needed.
import minidblite
db = minidblite.create_database("my_app.db")
db.new_table("users")
db.new_column("name", str)
db.new_column("age", int)
db.add("users", name="Ali", age=20)
db.get("users").decorate() # pretty-print to terminal
Installation
pip install minidblite
Requires Python 3.10+ and installs
tabulateautomatically.
Quick Start
import minidblite
# 1. Create (or open) a database
db = minidblite.create_database("shop.db")
# 2. Define a table
db.new_table("products")
db.new_column("name", str)
db.new_column("price", float)
db.new_column("stock", int)
# 3. Insert rows
db.add("products", name="Widget", price=9.99, stock=100)
db.add("products", name="Gadget", price=24.99, stock=50)
db.add("products", name="Doohickey", price=4.49, stock=200)
# 4. Display all rows
db.get("products").decorate()
# 5. Filter rows
cheap = db.get("products", name="Widget")
print(cheap.records) # [{'id': 1, 'name': 'Widget', ...}]
# 6. Update a row
db.update("products", row_id=1, price=8.99)
# 7. Delete a row
db.delete("products", name="Doohickey")
# 8. Export to JSON
db.export_json("products", path="products.json")
Context Manager
The database connection is closed automatically when used as a context manager:
with minidblite.create_database("temp.db") as db:
db.new_table("sessions")
db.new_column("token", str)
db.add("sessions", token="abc123")
# connection closed here
API Reference
minidblite.create_database(db_name="database_session.db") โ Database
Create or open a SQLite database.
| Parameter | Type | Default | Description |
|---|---|---|---|
db_name |
str |
"database_session.db" |
Filename or path. Use ":memory:"for an in-memory DB. |
Database.new_table(table_name)
Create a table (with an auto-increment id column).
db.new_table("orders")
Database.new_column(column_name, data_type, table_name=None)
Add a column to a table.
db.new_column("email", str) # uses last created table
db.new_column("score", float, table_name="players")
Supported Python types:
| Python | SQLite |
|---|---|
str |
TEXT |
int |
INTEGER |
float |
REAL |
bool |
INTEGER |
bytes |
BLOB |
Database.add(table_name, **columns) โ dict
Insert a row.
db.add("users", name="Ali", age=20)
# โ {'success': True, 'message': '...', 'data': {'id': 1}}
Database.get(table_name, **filters) โ QueryResult
Fetch rows, optionally filtered.
all_users = db.get("users")
ali = db.get("users", name="Ali")
QueryResult methods
| Method / Property | Description |
|---|---|
.decorate(tablefmt="rounded_outline") |
Pretty-print to terminal; returns the string |
.records |
list[dict]โ rows as dicts |
.rows |
list[tuple]โ raw tuples |
.columns |
list[str]โ column names |
.to_json(indent=2) |
Serialize to JSON string |
.to_csv() |
Serialize to CSV string |
len(result) |
Number of rows |
result[0] |
First row as dict |
for row in result |
Iterate over dicts |
Database.update(table_name, row_id, **new_values) โ dict
Update a row by its id.
db.update("users", row_id=1, name="Vali", age=21)
Database.delete(table_name, is_all=False, **filters) โ dict
Delete rows.
db.delete("users", name="Vali") # first match only
db.delete("users", is_all=True, age=20) # all matches
db.delete("users", is_all=True) # ALL rows in table
Database.schema(table_name) โ dict
Return column metadata.
info = db.schema("users")
# info['data']['columns'] โ [{'name': 'id', 'type': 'INTEGER', ...}, ...]
Database.export_json(table_name, path=None) โ dict
Export table data to JSON.
db.export_json("users", path="users.json") # write file
json_str = db.export_json("users")["data"] # get string
Database.list_tables() โ list[str]
Return all table names in the database.
Database.drop_table(table_name) โ dict
Drop a table permanently.
Database.close() โ dict
Close the connection. Called automatically by the context manager.
Structured Responses
Every write method (add, update, delete, new_table, new_column, โฆ)
returns a dict:
# Success
{'success': True, 'message': 'Row inserted ...', 'data': {'id': 3}}
# Failure (no crash)
{'success': False, 'message': 'Table "ghost" does not exist.', 'error': '...', 'data': None}
Logging
Enable Python's built-in logging to see what minidblite is doing:
import logging
logging.basicConfig(level=logging.INFO)
import minidblite
db = minidblite.create_database()
db.new_table("logs") # INFO:minidblite:Table 'logs' created ...
Running Tests
pip install minidblite[dev]
pytest tests/ -v
Project Structure
minidblite/
โโโ minidblite/
โ โโโ __init__.py โ public API
โ โโโ core.py โ create_database() factory
โ โโโ database.py โ Database class
โ โโโ formatter.py โ QueryResult & pretty-print
โ โโโ utils.py โ type mapping, validators, response builders
โโโ tests/
โ โโโ test_basic.py
โโโ pyproject.toml
โโโ README.md
โโโ LICENSE
โโโ .gitignore
License
MIT โ see LICENSE.
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 minidblite-0.1.1.tar.gz.
File metadata
- Download URL: minidblite-0.1.1.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2782257ecad0bb32996180aaa1fb85b828bbfa92b007db5b1d704cc08cdd9b2
|
|
| MD5 |
3179dbc5a54eb78c7f7eaf6242c4825f
|
|
| BLAKE2b-256 |
cc945559f657d33a8344fda9378bd7e3d3da3ccf32a7f550c5bbe8e419df822b
|
File details
Details for the file minidblite-0.1.1-py3-none-any.whl.
File metadata
- Download URL: minidblite-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4496e8634789a85ddf4f3a03d5c3b789e2eafd954f6542146eed4a904b50397
|
|
| MD5 |
e08c2d5e5ad74b9bdb97f33332af7773
|
|
| BLAKE2b-256 |
eb3a9d03fede932ff36116c713dc4b19acb6bf181eae6c22d2021ead8b3dc8a5
|