|
|
||
|---|---|---|
| .. | ||
| tools | ||
| utils | ||
| README.md | ||
| example.db | ||
| flow.py | ||
| main.py | ||
| nodes.py | ||
| requirements.txt | ||
README.md
SQLite Database with PocketFlow
This example demonstrates how to properly integrate SQLite database operations with PocketFlow, focusing on:
-
Clean code organization with separation of concerns:
- Tools layer for database operations (
tools/database.py) - Node implementation for PocketFlow integration (
nodes.py) - Flow configuration (
flow.py) - Safe SQL query execution with parameter binding
- Tools layer for database operations (
-
Best practices for database operations:
- Connection management with proper closing
- SQL injection prevention using parameterized queries
- Error handling and resource cleanup
- Simple schema management
-
Example task management system:
- Database initialization
- Task creation
- Task listing
- Status tracking
Project Structure
pocketflow-tool-database/
├── tools/
│ └── database.py # SQLite database operations
├── nodes.py # PocketFlow node implementation
├── flow.py # Flow configuration
└── main.py # Example usage
Setup
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
Usage
Run the example:
python main.py
This will:
- Initialize a SQLite database with a tasks table
- Create an example task
- List all tasks in the database
- Display the results
Key Concepts Demonstrated
-
Database Operations
- Safe connection handling
- Query parameterization
- Schema management
-
Code Organization
- Clear separation between database operations and PocketFlow components
- Modular project structure
- Type hints and documentation
-
PocketFlow Integration
- Node implementation with prep->exec->post lifecycle
- Flow configuration
- Shared store usage for data passing
Example Output
Database Status: Database initialized
Task Status: Task created successfully
All Tasks:
- ID: 1
Title: Example Task
Description: This is an example task created using PocketFlow
Status: pending
Created: 2024-03-02 12:34:56