0
0
LangChainframework~10 mins

Loading from databases in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Loading from databases
Start
Connect to DB
Run Query
Fetch Results
Create LangChain Docs
Use Docs in Chain
End
This flow shows how LangChain connects to a database, runs a query, fetches results, converts them into documents, and uses them in a chain.
Execution Sample
LangChain
from langchain_community.document_loaders import SQLDatabaseLoader
from langchain_community.utilities import SQLDatabase

db = SQLDatabase.from_uri('sqlite:///example.db')
loader = SQLDatabaseLoader(db=db, query="SELECT * FROM table")
docs = loader.load()
print(len(docs))
This code connects to a SQLite database, loads data from a SQL query as documents, and prints how many documents were loaded.
Execution Table
StepActionEvaluationResult
1Create SQLDatabase and SQLDatabaseLoader with DB URLdb = SQLDatabase.from_uri('sqlite:///example.db'); loader = SQLDatabaseLoader(db=db, query="SELECT * FROM table")Loader object ready
2Call loader.load()Runs SQL query to fetch rowsData rows fetched from DB
3Convert rows to LangChain Document objectsEach row becomes a DocumentList of Document objects created
4Print length of docslen(docs)Number of documents printed
5EndAll data loaded and ready for chainProcess complete
💡 All rows fetched and converted to documents, loading finished
Variable Tracker
VariableStartAfter Step 2After Step 3Final
loaderNoneSQLDatabaseLoader instanceSQLDatabaseLoader instanceSQLDatabaseLoader instance
docsNoneNoneList of Document objectsList of Document objects
len(docs)N/AN/AN/ANumber of documents
Key Moments - 2 Insights
Why do we need to convert database rows into Document objects?
LangChain works with Document objects to process text data. The execution_table step 3 shows rows converted to Documents so chains can use them.
What happens if the database URL is incorrect?
The loader creation or load() call will fail to connect or fetch data, stopping execution early before step 2 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 3?
AAn error message
BRaw database rows
CA list of Document objects
DEmpty list
💡 Hint
Check the 'Result' column in row for step 3 in execution_table
At which step does the actual database query run?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Evaluation' columns in execution_table for step 2
If the database has 100 rows, what will len(docs) be after loading?
A100
B1
C0
DDepends on the query
💡 Hint
Variable tracker shows docs is a list of Document objects created from rows
Concept Snapshot
Loading from databases in LangChain:
- Create SQLDatabase.from_uri & SQLDatabaseLoader(query, db)
- Call load() to fetch data
- Rows convert to Document objects
- Use docs in chains for processing
- Handles SQL databases easily
Full Transcript
This visual execution shows how LangChain loads data from a database. First, it creates SQLDatabase from URI and SQLDatabaseLoader with query and db. Then, calling load() runs a SQL query to fetch rows. These rows are converted into Document objects that LangChain can use. Finally, the number of documents is printed. This process lets you use database content in LangChain chains easily.