A command-line interface template for managing SQLite databases using sqlite-worker.
# Make executable
chmod +x cli.py
# Show help
./cli.py --help
# List items
./cli.py list
# Add item
./cli.py add "My Item" --desc "Description"- ✅ CRUD operations (Create, Read, Update, Delete)
- ✅ Search functionality
- ✅ JSON import/export
- ✅ Database statistics
- ✅ Thread-safe operations
- ✅ Clean command-line interface
./cli.py list
./cli.py list --limit 10./cli.py add "Item Name"
./cli.py add "Item Name" --desc "Description"./cli.py get 1./cli.py update 1 --name "New Name"
./cli.py update 1 --desc "New Description"
./cli.py update 1 --name "New Name" --desc "New Description"./cli.py delete 1./cli.py search "keyword"./cli.py export output.json./cli.py import data.json./cli.py statsUse a different database file:
./cli.py --db custom.db list
./cli.py --db custom.db add "Item"# Add some items
./cli.py add "Task 1" --desc "Complete documentation"
./cli.py add "Task 2" --desc "Write tests"
./cli.py add "Task 3" --desc "Deploy to production"
# List all items
./cli.py list
# Search for specific items
./cli.py search "test"
# Export to JSON
./cli.py export tasks.json
# Show statistics
./cli.py stats
# Update an item
./cli.py update 1 --desc "Documentation completed"
# Delete an item
./cli.py delete 2def your_command(self):
"""Your command implementation"""
# Your logic here
pass
# In main():
your_parser = subparsers.add_parser('your_command', help='Your help text')
your_parser.add_argument('arg', help='Argument help')# Update schema
self.worker.execute("""
ALTER TABLE items ADD COLUMN your_field TEXT
""")
# Update add_item
def add_item(self, name, description=None, your_field=None):
token = self.worker.insert("items", {
"name": name,
"description": description,
"your_field": your_field
})# Install colorama
# pip install colorama
from colorama import Fore, Style
def list_items(self):
print(f"{Fore.GREEN}✅ Items:{Style.RESET_ALL}")
# ..../cli.py add "Buy groceries" --desc "Milk, eggs, bread"
./cli.py add "Call dentist" --desc "Schedule appointment"
./cli.py list./cli.py add "Meeting Notes" --desc "Discussed Q4 goals..."
./cli.py search "meeting"./cli.py add "Laptop" --desc "Dell XPS 15, SN: 12345"
./cli.py add "Monitor" --desc "27 inch 4K"# Customize for contacts
self.worker.execute("""
CREATE TABLE contacts (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT,
phone TEXT
)
""")Create setup.py:
from setuptools import setup
setup(
name='your-cli-tool',
version='1.0.0',
py_modules=['cli'],
install_requires=['sqlite-worker'],
entry_points={
'console_scripts': [
'your-tool=cli:main',
],
},
)Install:
pip install -e .
your-tool listimport unittest
from cli import CLITool
class TestCLI(unittest.TestCase):
def setUp(self):
self.tool = CLITool(':memory:')
def test_add_item(self):
self.tool.add_item("Test", "Description")
items = self.tool.worker.fetch_results(
self.tool.worker.select("items")
)
self.assertEqual(len(items), 1)def delete_item(self, item_id):
response = input(f"Delete item #{item_id}? (y/N): ")
if response.lower() != 'y':
print("Cancelled")
return
# ... delete logicfrom tqdm import tqdm
def import_json(self, filename):
data = json.load(open(filename))
for item in tqdm(data, desc="Importing"):
self.worker.insert("items", item)import configparser
config = configparser.ConfigParser()
config.read('config.ini')
db_path = config.get('database', 'path', fallback='data.db')Permission Denied
chmod +x cli.pyDatabase Locked
- Ensure no other process is using the database
- WAL mode is enabled by default for better concurrency
Module Not Found
pip install sqlite-worker- argparse Documentation
- sqlite-worker Repository
- Click Framework - Alternative CLI framework
- Add authentication
- Implement backup/restore
- Add shell completion
- Create man pages
- Package as executable
- Add logging
- Implement undo/redo
- Add bulk operations
Perfect starting point for building command-line tools with SQLite!