# SQLDatabaseToolkit

> **Class** in `langchain_community`

📖 [View in docs](https://reference.langchain.com/python/langchain-community/agent_toolkits/sql/toolkit/SQLDatabaseToolkit)

SQLDatabaseToolkit for interacting with SQL databases.

## Signature

```python
SQLDatabaseToolkit()
```

## Description

**Setup:**

Install ``langchain-community``.

.. code-block:: bash

    pip install -U langchain-community

**Key init args:**

db: SQLDatabase
    The SQL database.
llm: BaseLanguageModel
    The language model (for use with QuerySQLCheckerTool)

**Instantiate:**

.. code-block:: python

from langchain_community.agent_toolkits.sql.toolkit import SQLDatabaseToolkit
from langchain_community.utilities.sql_database import SQLDatabase
from langchain_openai import ChatOpenAI

db = SQLDatabase.from_uri("sqlite:///Chinook.db")
llm = ChatOpenAI(temperature=0)

toolkit = SQLDatabaseToolkit(db=db, llm=llm)

**Tools:**

.. code-block:: python

toolkit.get_tools()

**Use within an agent:**

.. code-block:: python

from langchain import hub
from langgraph.prebuilt import create_react_agent

# Pull prompt (or define your own)
prompt_template = hub.pull("langchain-ai/sql-agent-system-prompt")
system_message = prompt_template.format(dialect="SQLite", top_k=5)

# Create agent
agent_executor = create_react_agent(
    llm, toolkit.get_tools(), state_modifier=system_message
)

# Query agent
example_query = "Which country's customers spent the most?"

events = agent_executor.stream(
    {"messages": [("user", example_query)]},
    stream_mode="values",
)
for event in events:
    event["messages"][-1].pretty_print()

## Extends

- `BaseToolkit`

## Properties

- `db`
- `llm`
- `dialect`
- `model_config`

## Methods

- [`get_tools()`](https://reference.langchain.com/python/langchain-community/agent_toolkits/sql/toolkit/SQLDatabaseToolkit/get_tools)
- [`get_context()`](https://reference.langchain.com/python/langchain-community/agent_toolkits/sql/toolkit/SQLDatabaseToolkit/get_context)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-community/blob/a6a6079511ac8a5c1293337f88096b8641562e77/libs/community/langchain_community/agent_toolkits/sql/toolkit.py#L24)