SQLDatabase#
- class langchain_community.utilities.sql_database.SQLDatabase(engine: Engine, schema: str | None = None, metadata: MetaData | None = None, ignore_tables: List[str] | None = None, include_tables: List[str] | None = None, sample_rows_in_table_info: int = 3, indexes_in_table_info: bool = False, custom_table_info: dict | None = 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 since version langchain-community==0.0.1: Use
get_usable_table_names
instead.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: str | None = None, metadata: MetaData | None = None, ignore_tables: List[str] | None = None, include_tables: List[str] | None = None, sample_rows_in_table_info: int = 3, indexes_in_table_info: bool = False, custom_table_info: dict | None = 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 (str | None) β
metadata (MetaData | None) β
ignore_tables (List[str] | None) β
include_tables (List[str] | None) β
sample_rows_in_table_info (int) β
indexes_in_table_info (bool) β
custom_table_info (dict | None) β
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: str | None = None, api_token: str | None = None, warehouse_id: str | None = None, cluster_id: str | None = None, engine_args: dict | None = 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 | URL, engine_args: dict | None = None, **kwargs: Any) SQLDatabase [source]#
Construct a SQLAlchemy engine from URI.
- Parameters:
database_uri (str | URL) β
engine_args (dict | None) β
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: List[str] | None = 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 (List[str] | None) β
- Return type:
str
- get_table_info_no_throw(table_names: List[str] | None = 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 (List[str] | None) β
- Return type:
str
- get_table_names() Iterable[str] [source]#
Deprecated since version langchain-community==0.0.1: Use
get_usable_table_names
instead.Get names of tables available.
- Return type:
Iterable[str]
- get_usable_table_names() Iterable[str] [source]#
Get names of tables available.
- Return type:
Iterable[str]
- run(command: str | Executable, fetch: Literal['all', 'one', 'cursor'] = 'all', include_columns: bool = False, *, parameters: Dict[str, Any] | None = None, execution_options: Dict[str, Any] | None = None) 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 (str | Executable) β
fetch (Literal['all', 'one', 'cursor']) β
include_columns (bool) β
parameters (Dict[str, Any] | None) β
execution_options (Dict[str, Any] | None) β
- Return type:
str | Sequence[Dict[str, Any]] | Result[Any]
- run_no_throw(command: str, fetch: Literal['all', 'one'] = 'all', include_columns: bool = False, *, parameters: Dict[str, Any] | None = None, execution_options: Dict[str, Any] | None = None) 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 (Dict[str, Any] | None) β
execution_options (Dict[str, Any] | None) β
- Return type:
str | Sequence[Dict[str, Any]] | Result[Any]
Examples using SQLDatabase