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.

Diagram

SQLiteTool Flow

👤 User

🤖 Agent

🔧 SQLiteTool

✅ Result

Installation

bash
pip install "praisonai[tools]"

No additional dependencies required – SQLite is built into Python!

Quick Start

  1. 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)
  2. 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

python
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.

python
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).

python
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.

python
tables = sqlite.list_tables()

Configuration Options

python
sqlite = SQLiteTool(
    path="mydata.db",      # Database file path
    timeout=30             # Connection timeout
)

Function-Based Usage

python
from praisonai_tools import query_sqlite

# Quick query
results = query_sqlite("SELECT * FROM users", path="mydata.db")

In-Memory Database

python
# Use :memory: for temporary in-memory database
sqlite = SQLiteTool(path=":memory:")
sqlite.execute("CREATE TABLE test (id INTEGER, value TEXT)")

Error Handling

python
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

ErrorCauseSolution
no such tableTable doesn’t existCreate table first
database is lockedConcurrent accessClose other connections
disk I/O errorFile permission issueCheck file permissions

How It Works

Diagram
SQLiteToolAgentUserSQLiteToolAgentUserRequestCall toolQueryDataResultResponse

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.


Related Tools