langchain_community.document_loaders.sql_database.SQLDatabaseLoader¶

class langchain_community.document_loaders.sql_database.SQLDatabaseLoader(query: Union[str, Select], db: SQLDatabase, *, parameters: Optional[Dict[str, Any]] = None, page_content_mapper: Optional[Callable[[...], str]] = None, metadata_mapper: Optional[Callable[[...], Dict[str, Any]]] = None, source_columns: Optional[Sequence[str]] = None, include_rownum_into_metadata: bool = False, include_query_into_metadata: bool = False)[source]¶

Load documents by querying database tables supported by SQLAlchemy.

For talking to the database, the document loader uses the SQLDatabase utility from the LangChain integration toolkit.

Each document represents one row of the result.

Parameters
  • query (Union[str, Select]) – The query to execute.

  • db (SQLDatabase) – A LangChain SQLDatabase, wrapping an SQLAlchemy engine.

  • sqlalchemy_kwargs – More keyword arguments for SQLAlchemy’s create_engine.

  • parameters (Optional[Dict[str, Any]]) – Optional. Parameters to pass to the query.

  • page_content_mapper (Optional[Callable[[...], str]]) – Optional. Function to convert a row into a string to use as the page_content of the document. By default, the loader serializes the whole row into a string, including all columns.

  • metadata_mapper (Optional[Callable[[...], Dict[str, Any]]]) – Optional. Function to convert a row into a dictionary to use as the metadata of the document. By default, no columns are selected into the metadata dictionary.

  • source_columns (Optional[Sequence[str]]) – Optional. The names of the columns to use as the source within the metadata dictionary.

  • include_rownum_into_metadata (bool) – Optional. Whether to include the row number into the metadata dictionary. Default: False.

  • include_query_into_metadata (bool) – Optional. Whether to include the query expression into the metadata dictionary. Default: False.

Methods

__init__(query, db, *[, parameters, ...])

param query

The query to execute.

alazy_load()

A lazy loader for Documents.

lazy_load()

A lazy loader for Documents.

load()

Load data into Document objects.

load_and_split([text_splitter])

Load Documents and split into chunks.

metadata_default_mapper(row[, column_names])

A reasonable default function to convert a record into a "metadata" dictionary.

page_content_default_mapper(row[, column_names])

A reasonable default function to convert a record into a "page content" string.

__init__(query: Union[str, Select], db: SQLDatabase, *, parameters: Optional[Dict[str, Any]] = None, page_content_mapper: Optional[Callable[[...], str]] = None, metadata_mapper: Optional[Callable[[...], Dict[str, Any]]] = None, source_columns: Optional[Sequence[str]] = None, include_rownum_into_metadata: bool = False, include_query_into_metadata: bool = False)[source]¶
Parameters
  • query (Union[str, Select]) – The query to execute.

  • db (SQLDatabase) – A LangChain SQLDatabase, wrapping an SQLAlchemy engine.

  • sqlalchemy_kwargs – More keyword arguments for SQLAlchemy’s create_engine.

  • parameters (Optional[Dict[str, Any]]) – Optional. Parameters to pass to the query.

  • page_content_mapper (Optional[Callable[[...], str]]) – Optional. Function to convert a row into a string to use as the page_content of the document. By default, the loader serializes the whole row into a string, including all columns.

  • metadata_mapper (Optional[Callable[[...], Dict[str, Any]]]) – Optional. Function to convert a row into a dictionary to use as the metadata of the document. By default, no columns are selected into the metadata dictionary.

  • source_columns (Optional[Sequence[str]]) – Optional. The names of the columns to use as the source within the metadata dictionary.

  • include_rownum_into_metadata (bool) – Optional. Whether to include the row number into the metadata dictionary. Default: False.

  • include_query_into_metadata (bool) – Optional. Whether to include the query expression into the metadata dictionary. Default: False.

async alazy_load() AsyncIterator[Document]¶

A lazy loader for Documents.

Return type

AsyncIterator[Document]

lazy_load() Iterator[Document][source]¶

A lazy loader for Documents.

Return type

Iterator[Document]

load() List[Document]¶

Load data into Document objects.

Return type

List[Document]

load_and_split(text_splitter: Optional[TextSplitter] = None) List[Document]¶

Load Documents and split into chunks. Chunks are returned as Documents.

Do not override this method. It should be considered to be deprecated!

Parameters

text_splitter (Optional[TextSplitter]) – TextSplitter instance to use for splitting documents. Defaults to RecursiveCharacterTextSplitter.

Returns

List of Documents.

Return type

List[Document]

static metadata_default_mapper(row: RowMapping, column_names: Optional[List[str]] = None) Dict[str, Any][source]¶

A reasonable default function to convert a record into a “metadata” dictionary.

Parameters
  • row (RowMapping) –

  • column_names (Optional[List[str]]) –

Return type

Dict[str, Any]

static page_content_default_mapper(row: RowMapping, column_names: Optional[List[str]] = None) str[source]¶

A reasonable default function to convert a record into a “page content” string.

Parameters
  • row (RowMapping) –

  • column_names (Optional[List[str]]) –

Return type

str