# WatsonxSQLDatabase

> **Class** in `langchain_ibm`

📖 [View in docs](https://reference.langchain.com/python/langchain-ibm/utilities/sql_database/WatsonxSQLDatabase)

Watsonx SQL Database class for IBM watsonx.ai databases connection assets.

Uses Arrow Flight to interact with databases via watsonx.

## Signature

```python
WatsonxSQLDatabase(
    self,
    *,
    connection_id: str,
    schema: str,
    project_id: str | None = None,
    space_id: str | None = None,
    url: str | None = None,
    api_key: str | None = None,
    apikey: str | None = None,
    token: str | None = None,
    password: str | None = None,
    username: str | None = None,
    instance_id: str | None = None,
    version: str | None = None,
    verify: str | bool | None = None,
    watsonx_client: APIClient | None = None,
    ignore_tables: list[str] | None = None,
    include_tables: list[str] | None = None,
    sample_rows_in_table_info: int = 3,
    max_string_length: int = 300,
)
```

## Description

???+ info "Setup"

    To use, you should have `langchain_ibm` python package installed,
    and the environment variable `WATSONX_API_KEY` set with your API key, or pass
    it as a named parameter `api_key` to the constructor.

    ```bash
    pip install -U langchain-ibm

    # or using uv
    uv add langchain-ibm
    ```

    ```bash
    export WATSONX_API_KEY="your-api-key"
    ```

    !!! deprecated
        `apikey` and `WATSONX_APIKEY` are deprecated and will be removed in
        version `2.0.0`. Use `api_key` and `WATSONX_API_KEY` instead.

??? info "Instantiate"

    ```python
    from langchain_ibm.utilities.sql_database import WatsonxSQLDatabase

    wx_sql_database = WatsonxSQLDatabase(
        connection_id="<CONNECTION_ID>",
        schema="<SCHEMA>",
        url="<URL>",
        project_id="<PROJECT_ID>",
        api_key="<API_KEY>",
    )
    ```

??? warning "Raises"
    - `ValueError` - if some required credentials are missing
    - `RuntimeError` - if no tables found in given schema

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `connection_id` | `str` | Yes | ID of db connection asset |
| `schema` | `str` | Yes | name of the database schema from which tables will be read |
| `project_id` | `str \| None` | No | ID of project (default: `None`) |
| `space_id` | `str \| None` | No | ID of space (default: `None`) |
| `url` | `str \| None` | No | URL to the Watson Machine Learning or CPD instance (default: `None`) |
| `api_key` | `str \| None` | No | API key to the Watson Machine Learning or CPD instance (default: `None`) |
| `apikey` | `str \| None` | No | API key to the Watson Machine Learning or CPD instance (deprecated) (default: `None`) |
| `token` | `str \| None` | No | service token, used in token authentication (default: `None`) |
| `password` | `str \| None` | No | password to the CPD instance (default: `None`) |
| `username` | `str \| None` | No | username to the CPD instance (default: `None`) |
| `instance_id` | `str \| None` | No | instance_id of the CPD instance (default: `None`) |
| `version` | `str \| None` | No | version of the CPD instance (default: `None`) |
| `verify` | `str \| bool \| None` | No | certificate verification flag (default: `None`) |
| `watsonx_client` | `APIClient \| None` | No | instance of `ibm_watsonx_ai.APIClient` (default: `None`) |
| `ignore_tables` | `list[str] \| None` | No | list of tables that will be ignored (default: `None`) |
| `include_tables` | `list[str] \| None` | No | list of tables that should be included (default: `None`) |
| `sample_rows_in_table_info` | `int` | No | number of first rows to be added to the table info (default: `3`) |
| `max_string_length` | `int` | No | max length of string (default: `300`) |

## Constructors

```python
__init__(
    self,
    *,
    connection_id: str,
    schema: str,
    project_id: str | None = None,
    space_id: str | None = None,
    url: str | None = None,
    api_key: str | None = None,
    apikey: str | None = None,
    token: str | None = None,
    password: str | None = None,
    username: str | None = None,
    instance_id: str | None = None,
    version: str | None = None,
    verify: str | bool | None = None,
    watsonx_client: APIClient | None = None,
    ignore_tables: list[str] | None = None,
    include_tables: list[str] | None = None,
    sample_rows_in_table_info: int = 3,
    max_string_length: int = 300,
) -> None
```

| Name | Type |
|------|------|
| `connection_id` | `str` |
| `schema` | `str` |
| `project_id` | `str \| None` |
| `space_id` | `str \| None` |
| `url` | `str \| None` |
| `api_key` | `str \| None` |
| `apikey` | `str \| None` |
| `token` | `str \| None` |
| `password` | `str \| None` |
| `username` | `str \| None` |
| `instance_id` | `str \| None` |
| `version` | `str \| None` |
| `verify` | `str \| bool \| None` |
| `watsonx_client` | `APIClient \| None` |
| `ignore_tables` | `list[str] \| None` |
| `include_tables` | `list[str] \| None` |
| `sample_rows_in_table_info` | `int` |
| `max_string_length` | `int` |


## Properties

- `schema`
- `watsonx_client`

## Methods

- [`get_usable_table_names()`](https://reference.langchain.com/python/langchain-ibm/utilities/sql_database/WatsonxSQLDatabase/get_usable_table_names)
- [`run()`](https://reference.langchain.com/python/langchain-ibm/utilities/sql_database/WatsonxSQLDatabase/run)
- [`run_no_throw()`](https://reference.langchain.com/python/langchain-ibm/utilities/sql_database/WatsonxSQLDatabase/run_no_throw)
- [`get_table_info()`](https://reference.langchain.com/python/langchain-ibm/utilities/sql_database/WatsonxSQLDatabase/get_table_info)
- [`get_table_info_no_throw()`](https://reference.langchain.com/python/langchain-ibm/utilities/sql_database/WatsonxSQLDatabase/get_table_info_no_throw)
- [`get_context()`](https://reference.langchain.com/python/langchain-ibm/utilities/sql_database/WatsonxSQLDatabase/get_context)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-ibm/blob/8a4e7ff49e9b316b266baa1bfdfb7a794694101a/libs/ibm/langchain_ibm/utilities/sql_database.py#L175)