Investment Plans workspace
Open raw ↗

# SQLite Database Template

Purpose: Template for SQLite database with WAL mode

Usage:

import sqlite3
from pathlib import Path

class Database:
    def __init__(self, db_path: str):
        self.db_path = Path(db_path)
        self.db_path.parent.mkdir(parents=True, exist_ok=True)
        self.conn = sqlite3.connect(str(self.db_path))
        self.conn.execute("PRAGMA journal_mode=WAL")
        self.conn.row_factory = sqlite3.Row
    
    def execute(self, sql: str, params: tuple = None):
        if params:
            return self.conn.execute(sql, params)
        return self.conn.execute(sql)
    
    def commit(self):
        self.conn.commit()

Notes:

Source: HAD Digital MVP (database.py)