Skip to main content

A lightweight, beginner-friendly SQLite wrapper for Python

Project description

minidblite ๐Ÿ—ƒ๏ธ

A lightweight, beginner-friendly SQLite wrapper for Python โ€” no SQL required.

Python License: MIT


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 tabulate automatically.


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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minidblite-0.1.1.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

minidblite-0.1.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

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

Hashes for minidblite-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a2782257ecad0bb32996180aaa1fb85b828bbfa92b007db5b1d704cc08cdd9b2
MD5 3179dbc5a54eb78c7f7eaf6242c4825f
BLAKE2b-256 cc945559f657d33a8344fda9378bd7e3d3da3ccf32a7f550c5bbe8e419df822b

See more details on using hashes here.

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

Hashes for minidblite-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c4496e8634789a85ddf4f3a03d5c3b789e2eafd954f6542146eed4a904b50397
MD5 e08c2d5e5ad74b9bdb97f33332af7773
BLAKE2b-256 eb3a9d03fede932ff36116c713dc4b19acb6bf181eae6c22d2021ead8b3dc8a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page