A simple SQLite3 wrapper for Python
Project description
Simple SQLite3
Simple SQLite3 is a lightweight, Pythonic wrapper for SQLite3 databases. It provides an intuitive and easy-to-use API for managing tables, inserting/querying data, and exporting results, with built-in support for JSON/CSV/TXT, schema evolution, and a convenient CLI.
Table of Contents
Features
- Easy-to-use API for SQLite3 database and table management.
- Command-line interface (CLI) for database operations.
- Support for exporting data to JSON, CSV and TXT formats.
- Robust to both nested and non-nested data, with datetime support.
- Utilities for processing queried results.
Installation
pip install simple-sqlite3
Quick Start
Programmatic Usage
1. Insert a List of Dicts (Non-Nested Data)
from simple_sqlite3 import Database
db = Database("database.db")
table = db.table("people")
table.insert([
{"name": "Amy", "age": 30, "city": "Helsinki"},
{"name": "Bob", "age": 25, "city": "Cambridge"},
{"name": "Cat", "age": 20, "city": "Paris"},
])
results = table.query("SELECT *")
print(results)
Example Output:
[
{'name': 'Amy', 'age': 30, 'city': 'Helsinki'},
{'name': 'Bob', 'age': 25, 'city': 'Cambridge'},
{'name': 'Cat', 'age': 20, 'city': 'Paris'}
]
2. Insert a List of Dicts (Non-Nested Data)
from simple_sqlite3 import Database
db = Database("database.db")
table = db.table("nested")
table.insert([
{
"country": "Finland",
"info": {
"capital": "Helsinki",
"latitude": 60.1699,
"longitude": 24.9384
}
},
{
"country": "France",
"info": {
"capital": "Paris",
"latitude": 48.8566,
"longitude": 2.3522
}
},
{
"country": "Japan",
"info": {
"capital": "Tokyo",
"latitude": 35.6895,
"longitude": 139.6917
}
}
])
results = table.query("SELECT *")
print(results)
3. Insert Timeseries
from datetime import datetime as dt
from simple_sqlite3 import Database
db = Database("database.db")
table = db.table("timeseries")
table.insert([
{"date": dt(2024, 6, 1), "value": 1.2345, "pair": "EURUSD"},
{"date": dt(2024, 6, 2), "value": 1.2350, "pair": "EURUSD"},
{"date": dt(2024, 6, 3), "value": 1.2360, "pair": "EURUSD"},
{"date": dt(2024, 6, 4), "value": 1.2375, "pair": "EURUSD"},
{"date": dt(2024, 6, 1), "value": 109.45, "pair": "USDJPY"},
{"date": dt(2024, 6, 2), "value": 109.60, "pair": "USDJPY"},
{"date": dt(2024, 6, 3), "value": 109.75, "pair": "USDJPY"},
{"date": dt(2024, 6, 4), "value": 109.90, "pair": "USDJPY"},
])
results = table.query("SELECT * WHERE pair = 'EURUSD'", auto_parse_dates=True)
print(results)
4. Insert Mixed Data
from datetime import datetime as dt
from simple_sqlite3 import Database
db = Database("database.db")
table = db.table("mixed_data")
table.insert([
{"date": dt(2024, 6, 1), "value": 1.2345, "pair": "EURUSD", "source": "ECB"},
{"date": dt(2024, 6, 1), "value": 109.45, "pair": "USDJPY", "source": "BOJ"},
{"date": dt(2024, 6, 2), "value": 0.8567, "pair": "EURGBP", "source": "ECB"},
{"date": dt(2024, 6, 2), "value": 1.4200, "pair": "GBPUSD", "source": "FED"},
{
"date": dt(2024, 6, 2),
"value": 1.2370,
"pair": "EURUSD",
"source": "ECB",
"meta": {
"confidence": 0.98,
"contributors": ["ECB", "Bloomberg"],
"valuation": {"buy": 0.4, "hold": 0.2, "sell": 0.4}
}
},
{
"date": dt(2024, 6, 3),
"value": 109.80,
"pair": "USDJPY",
"source": "BOJ",
"meta": {
"confidence": 0.95,
"contributors": ["BOJ"]
}
}
])
results = table.query("SELECT *", auto_parse_dates=True)
print(results)
5. Insert Data Into Memory and Export as JSON, CSV and TXT
from datetime import datetime as dt
from simple_sqlite3 import Database
db = Database(":memory:")
table = db.table("timeseries")
table.insert([
{"date": dt(2025, 5, 22), "value": 5328, "idx": "S&P 500"},
{"date": dt(2025, 5, 21), "value": 5421, "idx": "S&P 500"},
{"date": dt(2025, 5, 22), "value": 5448, "idx": "EURO STOXX 50"},
{"date": dt(2025, 5, 21), "value": 5452, "idx": "EURO STOXX 50"},
])
table.export_to_json("timeseries.json")
table.export_to_csv("timeseries.csv")
table.export_to_txt("timeseries.txt")
6. Exporting Queried Results
from datetime import datetime as dt
from simple_sqlite3 import Database
from simple_sqlite3.utils import QueryResultsProcessor
db = Database(":memory:")
table = db.table("timeseries")
table.insert([
{"date": dt(2025, 5, 22), "value": 5328, "idx": "S&P 500"},
{"date": dt(2025, 5, 21), "value": 5421, "idx": "S&P 500"},
{"date": dt(2025, 5, 22), "value": 5448, "idx": "EURO STOXX 50"},
{"date": dt(2025, 5, 21), "value": 5452, "idx": "EURO STOXX 50"},
])
results = table.query("SELECT * WHERE idx = 'EURO STOXX 50'")
processor = QueryResultsProcessor(results)
processor.to_json("timeseries.json")
processor.to_csv("timeseries.csv")
processor.to_txt("timeseries.txt")
7. Vacuuming and Cleaning Up
from datetime import datetime as dt
from simple_sqlite3 import Database
db = Database("database.db")
table = db.table("stocks")
table.insert([
{"date": dt(2025, 5, 22), "value": 5328, "idx": "S&P 500"},
{"date": dt(2025, 5, 21), "value": 5421, "idx": "S&P 500"},
{"date": dt(2025, 5, 22), "value": 5448, "idx": "EURO STOXX 50"},
{"date": dt(2025, 5, 21), "value": 5451, "idx": "EURO STOXX 50"},
] * 2) # Duplicated
table.delete_duplicates()
table = db.table("currencies")
table.insert([
{"date": dt(2024, 6, 1), "value": 1.2345, "pair": "EURUSD"},
{"date": dt(2024, 6, 2), "value": 1.2350, "pair": "EURUSD"},
{"date": dt(2024, 6, 1), "value": 109.45, "pair": "USDJPY"},
{"date": dt(2024, 6, 2), "value": 109.60, "pair": "USDJPY"},
])
table.delete(confirm=True)
db.vacuum()
db.delete(confirm=True)
CLI Examples
Note: The CLI can be run using the
dbcommand after installation (e.g.,db insert ...).
If installed as a module, you can also usepython -m simple_sqlite3.cli ....
Insert data from a JSON file into a table
db insert -database database.db -table timeseries -file timeseries.json
Query all rows from a table
db query -database database.db -table timeseries -sql "SELECT *"
Remove duplicate rows from a table
db delete_duplicates -database database.db -table timeseries
Export a table to CSV format
db export -database database.db -table timeseries -output timeseries.csv
Delete a table from the database
db delete -database database.db -table timeseries
Delete the entire database file
db delete -database database.db
Advanced Features
- Automatic WAL Mode: Write-Ahead Logging for better concurrency (default).
- Schema Evolution: New columns are added automatically on insert if
force=True(default). - Batch Export: Efficiently export large tables in batches to avoid memory issues.
License
This project is developed by Rob Suomi and licensed under the MIT License.
See the LICENSE file for details.
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 simple_sqlite3-0.0.2.tar.gz.
File metadata
- Download URL: simple_sqlite3-0.0.2.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
167267cb1127c1883f77a4e1352c2534e0918c0a49397030ae2fa93c7a7a7546
|
|
| MD5 |
5b8b9bd3fd01a4388b178a4687ef267f
|
|
| BLAKE2b-256 |
dbf6542c49bf548126cd57226d2e3bcd80b07e1b981cc157499227176cb526a9
|
File details
Details for the file simple_sqlite3-0.0.2-py3-none-any.whl.
File metadata
- Download URL: simple_sqlite3-0.0.2-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d81adcd3fc02ce29187b20a3fb54acbcd05a90947e321775d83d0eb51d427fab
|
|
| MD5 |
0a1c13ce5024699efcd2e760c9bf2c1c
|
|
| BLAKE2b-256 |
ee8c216d129c08b940724f499c07d15815041ce0f152e0ea2c11e9f8610d8d10
|