Bird
0
0

You want to fetch all users from a database asynchronously using the databases library in FastAPI. Which code snippet correctly fetches all rows and returns them as a list of dictionaries?

hard🚀 Application Q15 of 15
FastAPI - Database Integration
You want to fetch all users from a database asynchronously using the databases library in FastAPI. Which code snippet correctly fetches all rows and returns them as a list of dictionaries?
database = databases.Database('sqlite:///test.db')

async def get_users():
    query = 'SELECT * FROM users'
    # Which line correctly fetches all rows?
    ???
    return users
Ausers = await database.fetch_all(query)
Busers = database.fetch_all(query)
Cusers = await database.fetch_one(query)
Dusers = database.fetch_one(query)
Step-by-Step Solution
Solution:
  1. Step 1: Understand fetch_all vs fetch_one

    fetch_all returns all rows as a list; fetch_one returns a single row.
  2. Step 2: Use await with async fetch_all

    Since fetch_all is async, it must be awaited to get the result.
  3. Final Answer:

    users = await database.fetch_all(query) -> Option A
  4. Quick Check:

    Fetch all rows async with await = B [OK]
Quick Trick: Use await with fetch_all to get all rows asynchronously [OK]
Common Mistakes:
MISTAKES
  • Using fetch_one to get all rows
  • Forgetting await on async calls
  • Calling fetch_all synchronously

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes