WatsonxSQLDatabase#

class langchain_ibm.utilities.sql_database.WatsonxSQLDatabase(
*,
connection_id: str,
schema: str,
project_id: str | None = None,
space_id: str | None = None,
url: 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,
)[source]#

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

Uses Arrow Flight to interact with databases via watsonx.

Parameters:
  • connection_id (str) – ID of db connection asset

  • schema (str) – name of the database schema from which tables will be read

  • project_id (str | None) – ID of project

  • space_id (str | None) – ID of space

  • url (str | None) – URL to the Watson Machine Learning or CPD instance

  • apikey (str | None) – API key to the Watson Machine Learning or CPD instance

  • token (str | None) – service token, used in token authentication

  • password (str | None) – password to the CPD instance

  • username (str | None) – username to the CPD instance

  • instance_id (str | None) – instance_id of the CPD instance

  • version (str | None) – version of the CPD instance

  • verify (str | bool | None) – certificate verification flag

  • watsonx_client (APIClient | None) – instance of ibm_watsonx_ai.APIClient

  • ignore_tables (list[str] | None) – list of tables that will be ignored

  • include_tables (list[str] | None) – list of tables that should be included

  • sample_rows_in_table_info (int) – number of first rows to be added to the table info

  • max_string_length (int) – max length of string

???+ info “Setup”

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

```bash pip install -U langchain-ibm

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

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

??? info “Instantiate”

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

wx_sql_database = WatsonxSQLDatabase(

connection_id=”<CONNECTION_ID>”, schema=”<SCHEMA>”, url=credentials[“url”], project_id=project_id, apikey=credentials[“apikey”],

??? warning “Raises”
  • ValueError - if some required credentials are missing

  • RuntimeError - if no tables found in given schema

WatsonxSQLDatabase class.

Methods

__init__(*, connection_id, schema[, ...])

WatsonxSQLDatabase class.

get_context()

Return db context that you may want in agent prompt.

get_table_info([table_names])

Get information about specified tables.

get_table_info_no_throw([table_names])

Get information about specified tables.

get_usable_table_names()

Get names of tables available.

run(command, *[, include_columns])

Execute a SQL command and return a string representing the results.

run_no_throw(command, *[, include_columns])

Execute a SQL command and return a string representing the results.

__init__(
*,
connection_id: str,
schema: str,
project_id: str | None = None,
space_id: str | None = None,
url: 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[source]#

WatsonxSQLDatabase class.

Parameters:
  • connection_id (str)

  • schema (str)

  • project_id (str | None)

  • space_id (str | None)

  • url (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)

Return type:

None

get_context() dict[str, Any][source]#

Return db context that you may want in agent prompt.

Return type:

dict[str, Any]

get_table_info(
table_names: Iterable[str] | None = None,
) str[source]#

Get information about specified tables.

Parameters:

table_names (Iterable[str] | None)

Return type:

str

get_table_info_no_throw(
table_names: Iterable[str] | None = None,
) str[source]#

Get information about specified tables.

Parameters:

table_names (Iterable[str] | None)

Return type:

str

get_usable_table_names() Iterable[str][source]#

Get names of tables available.

Return type:

Iterable[str]

run(
command: str,
*,
include_columns: bool = False,
) str[source]#

Execute a SQL command and return a string representing the results.

Parameters:
  • command (str)

  • include_columns (bool)

Return type:

str

run_no_throw(
command: str,
*,
include_columns: bool = False,
) str[source]#

Execute a SQL command and return a string representing the results.

If the statement throws an error, the error message is returned.

Parameters:
  • command (str)

  • include_columns (bool)

Return type:

str