0
0
FastAPIframework~20 mins

Async database with databases library in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Database Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this FastAPI async database query?
Consider this FastAPI endpoint using the databases library to fetch all users asynchronously. What will the response contain?
FastAPI
from fastapi import FastAPI
import databases

DATABASE_URL = "sqlite+aiosqlite:///./test.db"
database = databases.Database(DATABASE_URL)
app = FastAPI()

@app.on_event("startup")
async def startup():
    await database.connect()

@app.on_event("shutdown")
async def shutdown():
    await database.disconnect()

@app.get("/users")
async def read_users():
    query = "SELECT * FROM users"
    results = await database.fetch_all(query=query)
    return results
AAn empty list regardless of database content
BA single dictionary representing the first user row only
CA runtime error because fetch_all is not awaited
DA list of dictionaries, each representing a user row from the users table
Attempts:
2 left
💡 Hint
Remember that fetch_all returns all matching rows asynchronously.
📝 Syntax
intermediate
2:00remaining
Which option correctly initializes the databases.Database instance for PostgreSQL?
You want to connect asynchronously to a PostgreSQL database using the databases library. Which DATABASE_URL string is correct?
A"postgresql://user:password@localhost/dbname"
B"postgresql+psycopg2://user:password@localhost/dbname"
C"postgresql+asyncpg://user:password@localhost/dbname"
D"postgresql+aiopg://user:password@localhost/dbname"
Attempts:
2 left
💡 Hint
The databases library requires an async driver prefix for async support.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI app raise an error on startup with databases library?
Given this code snippet, why does the app fail to start with an error related to database connection?
FastAPI
from fastapi import FastAPI
import databases

DATABASE_URL = "sqlite+aiosqlite:///./test.db"
database = databases.Database(DATABASE_URL)
app = FastAPI()

@app.on_event("startup")
async def startup():
    await database.connect()

@app.on_event("shutdown")
async def shutdown():
    await database.disconnect()
AThe startup event handler is not async, so await is missing causing a runtime error
BThe database URL is invalid for SQLite
CThe shutdown event handler should not be async
DThe database instance is not created correctly
Attempts:
2 left
💡 Hint
Check if the database connect method is awaited properly.
state_output
advanced
2:00remaining
What is the value of 'user_count' after this async query?
Assuming the users table has 5 rows, what will be the value of user_count after running this code?
FastAPI
query = "SELECT COUNT(*) FROM users"
user_count = await database.fetch_val(query)
A5
B[5]
C{'count': 5}
DNone
Attempts:
2 left
💡 Hint
fetch_val returns the first column of the first row.
🧠 Conceptual
expert
2:00remaining
Which statement about transaction management with databases library is correct?
Select the correct statement about handling transactions asynchronously using the databases library in FastAPI.
ATransactions are automatically committed after each query without explicit control
BYou can use 'async with database.transaction():' to run multiple queries atomically
CThe databases library does not support transactions in async mode
DYou must manually call database.commit() after each query to save changes
Attempts:
2 left
💡 Hint
Think about Python's async context managers for transactions.