manage_database
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| manage_database [2025/05/30 13:30] – [Schema Design] eagleeyenebula | manage_database [2025/06/06 13:28] (current) – [Database Manager (SQL)] eagleeyenebula | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| **[[https:// | **[[https:// | ||
| The **AI Database Manager (SQL)** provides a robust, extensible interface for working with an **SQLite** database, offering a lightweight yet powerful solution for structured data storage and retrieval. Designed with automation in mind, it features built-in schema initialization, | The **AI Database Manager (SQL)** provides a robust, extensible interface for working with an **SQLite** database, offering a lightweight yet powerful solution for structured data storage and retrieval. Designed with automation in mind, it features built-in schema initialization, | ||
| + | |||
| + | {{youtube> | ||
| + | |||
| + | ------------------------------------------------------------- | ||
| In addition to storing structured logs, configuration parameters, and experiment metadata, the Database Manager includes optimized methods for saving and querying performance metrics, making it a key component in tracking experiment results, application telemetry, or AI model performance. Its modular design supports easy extension to other SQL engines if needed, and its integration-ready format makes it suitable for both standalone applications and larger AI or data processing pipelines. By combining automation, efficiency, and adaptability, | In addition to storing structured logs, configuration parameters, and experiment metadata, the Database Manager includes optimized methods for saving and querying performance metrics, making it a key component in tracking experiment results, application telemetry, or AI model performance. Its modular design supports easy extension to other SQL engines if needed, and its integration-ready format makes it suitable for both standalone applications and larger AI or data processing pipelines. By combining automation, efficiency, and adaptability, | ||
| Line 55: | Line 59: | ||
| ==== Initialization ==== | ==== Initialization ==== | ||
| - | The `__init__` method initializes the SQLite database connection and ensures the schema is pre-created. | + | The __init__ method initializes the SQLite database connection and ensures the schema is pre-created. |
| < | < | ||
| - | ```python | + | python |
| def __init__(self, | def __init__(self, | ||
| """ | """ | ||
| Line 68: | Line 72: | ||
| self.cursor = self.connection.cursor() | self.cursor = self.connection.cursor() | ||
| self._initialize_schema() | self._initialize_schema() | ||
| - | ``` | + | |
| </ | </ | ||
| Line 76: | Line 80: | ||
| < | < | ||
| - | ```python | + | python |
| def _initialize_schema(self): | def _initialize_schema(self): | ||
| """ | """ | ||
| Line 90: | Line 94: | ||
| ''' | ''' | ||
| self.connection.commit() | self.connection.commit() | ||
| - | ``` | + | |
| </ | </ | ||
| Customizable Schema Changes: | Customizable Schema Changes: | ||
| + | < | ||
| - Modify the `CREATE TABLE` SQL if additional tables or fields are required. | - Modify the `CREATE TABLE` SQL if additional tables or fields are required. | ||
| + | </ | ||
| ==== Metric Saving ==== | ==== Metric Saving ==== | ||
| Line 101: | Line 106: | ||
| < | < | ||
| - | ```python | + | python |
| def save_metrics(self, | def save_metrics(self, | ||
| """ | """ | ||
| Line 115: | Line 120: | ||
| ''', | ''', | ||
| self.connection.commit() | self.connection.commit() | ||
| - | ``` | + | |
| </ | </ | ||
| Line 123: | Line 128: | ||
| < | < | ||
| - | ```python | + | python |
| def close(self): | def close(self): | ||
| """ | """ | ||
| Line 130: | Line 135: | ||
| if self.connection: | if self.connection: | ||
| self.connection.close() | self.connection.close() | ||
| - | ``` | + | |
| </ | </ | ||
| Line 142: | Line 147: | ||
| < | < | ||
| - | ```python | + | python |
| from manage_database import DatabaseManagerSQL | from manage_database import DatabaseManagerSQL | ||
| Line 148: | Line 153: | ||
| db_manager = DatabaseManagerSQL(db_path=" | db_manager = DatabaseManagerSQL(db_path=" | ||
| print(" | print(" | ||
| - | ``` | + | |
| </ | </ | ||
| ==== Example 2: Saving Metrics into the Database ==== | ==== Example 2: Saving Metrics into the Database ==== | ||
| - | Insert key-value pairs of metrics into the `metrics` table programmatically. | + | Insert key-value pairs of metrics into the **metrics** table programmatically. |
| < | < | ||
| - | ```python | + | python |
| # Define metrics to save | # Define metrics to save | ||
| metrics = { | metrics = { | ||
| Line 167: | Line 172: | ||
| db_manager.save_metrics(metrics) | db_manager.save_metrics(metrics) | ||
| print(" | print(" | ||
| - | ``` | + | |
| </ | </ | ||
| Line 175: | Line 180: | ||
| < | < | ||
| - | ```python | + | python |
| # Close the database manager connection | # Close the database manager connection | ||
| db_manager.close() | db_manager.close() | ||
| print(" | print(" | ||
| - | ``` | + | |
| </ | </ | ||
| Line 187: | Line 192: | ||
| < | < | ||
| - | ```python | + | python |
| import time | import time | ||
| import random | import random | ||
| Line 199: | Line 204: | ||
| db_manager.save_metrics(metrics) | db_manager.save_metrics(metrics) | ||
| time.sleep(2) | time.sleep(2) | ||
| - | ``` | + | |
| </ | </ | ||
| Line 207: | Line 212: | ||
| < | < | ||
| - | ```python | + | python |
| def fetch_all_metrics(self): | def fetch_all_metrics(self): | ||
| """ | """ | ||
| Line 214: | Line 219: | ||
| self.cursor.execute(' | self.cursor.execute(' | ||
| return self.cursor.fetchall() | return self.cursor.fetchall() | ||
| - | ``` | + | |
| </ | </ | ||
| Line 220: | Line 225: | ||
| < | < | ||
| - | ```python | + | python |
| # Fetch and print all metrics | # Fetch and print all metrics | ||
| all_metrics = db_manager.fetch_all_metrics() | all_metrics = db_manager.fetch_all_metrics() | ||
| print(all_metrics) | print(all_metrics) | ||
| - | ``` | + | |
| </ | </ | ||
| Line 230: | Line 235: | ||
| 1. **Custom Metric Analysis**: | 1. **Custom Metric Analysis**: | ||
| - | | + | * Extend the schema and add queries for computing averages, maxes, or trends. |
| 2. **Batch Framework Integration**: | 2. **Batch Framework Integration**: | ||
| - | | + | * Integrate as the backend for AI monitoring or feedback-driven systems. |
| 3. **Error Resilience**: | 3. **Error Resilience**: | ||
| - | | + | * Extend the **save_metrics** method with exception handling for edge cases. |
| ===== Best Practices ===== | ===== Best Practices ===== | ||
| 1. **Ensure Connection Lifecycle**: | 1. **Ensure Connection Lifecycle**: | ||
| - | | + | * Always close the database connection using the `close()` method at the end of workflow execution. |
| 2. **Validate Metric Keys**: | 2. **Validate Metric Keys**: | ||
| - | | + | * Ensure all metric names follow a consistent naming scheme to avoid ambiguity. |
| 3. **Avoid Large Batches**: | 3. **Avoid Large Batches**: | ||
| - | Limit batch-inserts to avoid database locks or increased latency. | + | * Limit batch-inserts to avoid database locks or increased latency. |
| ===== Conclusion ===== | ===== Conclusion ===== | ||
manage_database.1748611824.txt.gz · Last modified: 2025/05/30 13:30 by eagleeyenebula
