SQLDatabaseLoader#

class langchain_community.document_loaders.sql_database.SQLDatabaseLoader(query: str | Select, db: SQLDatabase, *, parameters: Dict[str, Any] | None = None, page_content_mapper: Callable[[...], str] | None = None, metadata_mapper: Callable[[...], Dict[str, Any]] | None = None, source_columns: Sequence[str] | None = 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 (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 (Dict[str, Any] | None) – Optional. Parameters to pass to the query.

  • page_content_mapper (Callable[[...], str] | None) – 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 (Callable[[...], Dict[str, Any]] | None) – 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 (Sequence[str] | None) – 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.

aload()

Load data into Document objects.

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: str | Select, db: SQLDatabase, *, parameters: Dict[str, Any] | None = None, page_content_mapper: Callable[[...], str] | None = None, metadata_mapper: Callable[[...], Dict[str, Any]] | None = None, source_columns: Sequence[str] | None = None, include_rownum_into_metadata: bool = False, include_query_into_metadata: bool = False)[source]#
Parameters:
  • query (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 (Dict[str, Any] | None) – Optional. Parameters to pass to the query.

  • page_content_mapper (Callable[[...], str] | None) – 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 (Callable[[...], Dict[str, Any]] | None) – 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 (Sequence[str] | None) – 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]

async aload() List[Document]#

Load data into Document objects.

Return type:

List[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: TextSplitter | None = 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: List[str] | None = None) Dict[str, Any][source]#

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

Parameters:
  • row (RowMapping) –

  • column_names (List[str] | None) –

Return type:

Dict[str, Any]

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

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

Parameters:
  • row (RowMapping) –

  • column_names (List[str] | None) –

Return type:

str