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)[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.[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, include_columns])Execute a SQL command and return a string representing the results.
- __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)[source]¶
Create engine from database URI.
- 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.
- 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.
- 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.
- get_table_names() Iterable[str] [source]¶
[Deprecated] Get names of tables available.[Deprecated] Get names of tables available.
Notes
Deprecated since version 0.0.1: Use get_usable_table_name instead.
- run(command: str, fetch: Literal['all', 'one'] = 'all', include_columns: bool = False) str [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.
- run_no_throw(command: str, fetch: Literal['all', 'one'] = 'all', include_columns: bool = False) str [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.