langchain_community.utilities.sql_database.SQLDatabase¶
- class langchain_community.utilities.sql_database.SQLDatabase(engine: Engine, schema: Optional[str] = None, metadata: Optional[MetaData] = None, ignore_tables: Optional[List[str]] = None, include_tables: Optional[List[str]] = None, sample_rows_in_table_info: int = 3, indexes_in_table_info: bool = False, custom_table_info: Optional[dict] = None, view_support: bool = False, max_string_length: int = 300, lazy_table_reflection: bool = False)[source]¶
- SQLAlchemy wrapper around a database. - Create engine from database URI. - Attributes - dialect- Return string representation of dialect to use. - table_info- Information about all tables in the database. - Methods - __init__(engine[, schema, metadata, ...])- Create engine from database URI. - from_cnosdb([url, user, password, tenant, ...])- Class method to create an SQLDatabase instance from a CnosDB connection. - from_databricks(catalog, schema[, host, ...])- Class method to create an SQLDatabase instance from a Databricks connection. - from_uri(database_uri[, engine_args])- Construct a SQLAlchemy engine from URI. - 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. - [Deprecated] Get names of tables available. - Get names of tables available. - run(command[, fetch, include_columns, ...])- Execute a SQL command and return a string representing the results. - run_no_throw(command[, fetch, ...])- Execute a SQL command and return a string representing the results. - Parameters
- engine (Engine) â 
- schema (Optional[str]) â 
- metadata (Optional[MetaData]) â 
- ignore_tables (Optional[List[str]]) â 
- include_tables (Optional[List[str]]) â 
- sample_rows_in_table_info (int) â 
- indexes_in_table_info (bool) â 
- custom_table_info (Optional[dict]) â 
- view_support (bool) â 
- max_string_length (int) â 
- lazy_table_reflection (bool) â 
 
 - __init__(engine: Engine, schema: Optional[str] = None, metadata: Optional[MetaData] = None, ignore_tables: Optional[List[str]] = None, include_tables: Optional[List[str]] = None, sample_rows_in_table_info: int = 3, indexes_in_table_info: bool = False, custom_table_info: Optional[dict] = None, view_support: bool = False, max_string_length: int = 300, lazy_table_reflection: bool = False)[source]¶
- Create engine from database URI. - Parameters
- engine (Engine) â 
- schema (Optional[str]) â 
- metadata (Optional[MetaData]) â 
- ignore_tables (Optional[List[str]]) â 
- include_tables (Optional[List[str]]) â 
- sample_rows_in_table_info (int) â 
- indexes_in_table_info (bool) â 
- custom_table_info (Optional[dict]) â 
- view_support (bool) â 
- max_string_length (int) â 
- lazy_table_reflection (bool) â 
 
 
 - classmethod from_cnosdb(url: str = '127.0.0.1:8902', user: str = 'root', password: str = '', tenant: str = 'cnosdb', database: str = 'public') SQLDatabase[source]¶
- Class method to create an SQLDatabase instance from a CnosDB connection. This method requires the âcnos-connectorâ package. If not installed, it can be added using pip install cnos-connector. - Parameters
- url (str) â The HTTP connection host name and port number of the CnosDB service, excluding âhttp://â or âhttps://â, with a default value of â127.0.0.1:8902â. 
- user (str) â The username used to connect to the CnosDB service, with a default value of ârootâ. 
- password (str) â The password of the user connecting to the CnosDB service, with a default value of ââ. 
- tenant (str) â The name of the tenant used to connect to the CnosDB service, with a default value of âcnosdbâ. 
- database (str) â The name of the database in the CnosDB tenant. 
 
- Returns
- An instance of SQLDatabase configured with the provided CnosDB connection details. 
- Return type
 
 - classmethod from_databricks(catalog: str, schema: str, host: Optional[str] = None, api_token: Optional[str] = None, warehouse_id: Optional[str] = None, cluster_id: Optional[str] = None, engine_args: Optional[dict] = None, **kwargs: Any) SQLDatabase[source]¶
- Class method to create an SQLDatabase instance from a Databricks connection. This method requires the âdatabricks-sql-connectorâ package. If not installed, it can be added using pip install databricks-sql-connector. - Parameters
- catalog (str) â The catalog name in the Databricks database. 
- schema (str) â The schema name in the catalog. 
- host (Optional[str]) â The Databricks workspace hostname, excluding âhttps://â part. If not provided, it attempts to fetch from the environment variable âDATABRICKS_HOSTâ. If still unavailable and if running in a Databricks notebook, it defaults to the current workspace hostname. Defaults to None. 
- api_token (Optional[str]) â The Databricks personal access token for accessing the Databricks SQL warehouse or the cluster. If not provided, it attempts to fetch from âDATABRICKS_TOKENâ. If still unavailable and running in a Databricks notebook, a temporary token for the current user is generated. Defaults to None. 
- warehouse_id (Optional[str]) â The warehouse ID in the Databricks SQL. If provided, the method configures the connection to use this warehouse. Cannot be used with âcluster_idâ. Defaults to None. 
- cluster_id (Optional[str]) â The cluster ID in the Databricks Runtime. If provided, the method configures the connection to use this cluster. Cannot be used with âwarehouse_idâ. If running in a Databricks notebook and both âwarehouse_idâ and âcluster_idâ are None, it uses the ID of the cluster the notebook is attached to. Defaults to None. 
- engine_args (Optional[dict]) â The arguments to be used when connecting Databricks. Defaults to None. 
- **kwargs (Any) â Additional keyword arguments for the from_uri method. 
 
- Returns
- An instance of SQLDatabase configured with the provided
- Databricks connection details. 
 
- Return type
- Raises
- ValueError â If âdatabricks-sql-connectorâ is not found, or if both âwarehouse_idâ and âcluster_idâ are provided, or if neither âwarehouse_idâ nor âcluster_idâ are provided and itâs not executing inside a Databricks notebook. 
 
 - classmethod from_uri(database_uri: str, engine_args: Optional[dict] = None, **kwargs: Any) SQLDatabase[source]¶
- Construct a SQLAlchemy engine from URI. - Parameters
- database_uri (str) â 
- engine_args (Optional[dict]) â 
- kwargs (Any) â 
 
- Return type
 
 - 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: Optional[List[str]] = None) str[source]¶
- Get information about specified tables. - Follows best practices as specified in: Rajkumar et al, 2022 (https://arxiv.org/abs/2204.00498) - If sample_rows_in_table_info, the specified number of sample rows will be appended to each table description. This can increase performance as demonstrated in the paper. - Parameters
- table_names (Optional[List[str]]) â 
- Return type
- str 
 
 - get_table_info_no_throw(table_names: Optional[List[str]] = None) str[source]¶
- Get information about specified tables. - Follows best practices as specified in: Rajkumar et al, 2022 (https://arxiv.org/abs/2204.00498) - If sample_rows_in_table_info, the specified number of sample rows will be appended to each table description. This can increase performance as demonstrated in the paper. - Parameters
- table_names (Optional[List[str]]) â 
- Return type
- str 
 
 - get_table_names() Iterable[str][source]¶
- [Deprecated] Get names of tables available. - Notes - Deprecated since version 0.0.1: Use get_usable_table_name instead. - Return type
- Iterable[str] 
 
 - get_usable_table_names() Iterable[str][source]¶
- Get names of tables available. - Return type
- Iterable[str] 
 
 - run(command: Union[str, Executable], fetch: Literal['all', 'one', 'cursor'] = 'all', include_columns: bool = False, *, parameters: Optional[Dict[str, Any]] = None, execution_options: Optional[Dict[str, Any]] = None) Union[str, Sequence[Dict[str, Any]], Result[Any]][source]¶
- Execute a SQL command and return a string representing the results. - If the statement returns rows, a string of the results is returned. If the statement returns no rows, an empty string is returned. - Parameters
- command (Union[str, Executable]) â 
- fetch (Literal['all', 'one', 'cursor']) â 
- include_columns (bool) â 
- parameters (Optional[Dict[str, Any]]) â 
- execution_options (Optional[Dict[str, Any]]) â 
 
- Return type
- Union[str, Sequence[Dict[str, Any]], Result[Any]] 
 
 - run_no_throw(command: str, fetch: Literal['all', 'one'] = 'all', include_columns: bool = False, *, parameters: Optional[Dict[str, Any]] = None, execution_options: Optional[Dict[str, Any]] = None) Union[str, Sequence[Dict[str, Any]], Result[Any]][source]¶
- Execute a SQL command and return a string representing the results. - If the statement returns rows, a string of the results is returned. If the statement returns no rows, an empty string is returned. - If the statement throws an error, the error message is returned. - Parameters
- command (str) â 
- fetch (Literal['all', 'one']) â 
- include_columns (bool) â 
- parameters (Optional[Dict[str, Any]]) â 
- execution_options (Optional[Dict[str, Any]]) â 
 
- Return type
- Union[str, Sequence[Dict[str, Any]], Result[Any]]