SQLite
Query and manage SQLite databases
Overview
SQLite tool allows you to query and manage SQLite databases. No server required – perfect for local development and embedded applications.
The user asks a data question; the agent runs SQL against the database and returns rows.
Installation
pip install "praisonai[tools]"No additional dependencies required – SQLite is built into Python!
Quick Start
- Simple Usage
python from praisonai_tools import SQLiteTool # Initialize sqlite = SQLiteTool(path="mydata.db") # Query results = sqlite.query("SELECT * FROM users LIMIT 5") print(results) - With Configuration
Use the same tool with an agent — see Usage with Agent below, or pass env vars and options from the sections above.
Usage with Agent
from praisonaiagents import Agent
from praisonai_tools import SQLiteTool
sqlite = SQLiteTool(path="mydata.db")
agent = Agent(
name="DBAnalyst",
instructions="You are a database analyst. Use SQLite to query data.",
tools=[sqlite]
)
response = agent.chat("Show me all tables in the database")
print(response)Available Methods
query(sql)
Execute a SQL query.
from praisonai_tools import SQLiteTool
sqlite = SQLiteTool(path="mydata.db")
results = sqlite.query("SELECT * FROM users WHERE active = 1")execute(sql)
Execute a SQL statement (INSERT, UPDATE, DELETE, CREATE).
sqlite.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)")
sqlite.execute("INSERT INTO users (name) VALUES ('Alice')")list_tables()
List all tables in the database.
tables = sqlite.list_tables()Configuration Options
sqlite = SQLiteTool(
path="mydata.db", # Database file path
timeout=30 # Connection timeout
)Function-Based Usage
from praisonai_tools import query_sqlite
# Quick query
results = query_sqlite("SELECT * FROM users", path="mydata.db")In-Memory Database
# Use :memory: for temporary in-memory database
sqlite = SQLiteTool(path=":memory:")
sqlite.execute("CREATE TABLE test (id INTEGER, value TEXT)")Error Handling
from praisonai_tools import SQLiteTool
sqlite = SQLiteTool(path="mydata.db")
result = sqlite.query("SELECT * FROM users")
if isinstance(result, dict) and "error" in result:
print(f"Error: {result['error']}")
else:
for row in result:
print(row)Common Errors
| Error | Cause | Solution |
|---|---|---|
no such table | Table doesn’t exist | Create table first |
database is locked | Concurrent access | Close other connections |
disk I/O error | File permission issue | Check file permissions |
How It Works
Best Practices
Inspect the schema first
List tables and columns before querying so the agent writes valid SQL.
Use parameterised queries
Bind values instead of string-formatting them to avoid SQL injection.
Limit result size
Add LIMIT so the agent works with a focused result set.