0
0
FastAPIframework~10 mins

Async database queries in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an async function in FastAPI.

FastAPI
async def [1]():
    return {"message": "Hello"}
Drag options to blanks, or click blank then click option'
Aget_data
Bfetch
Cread
Dprocess
Attempts:
3 left
💡 Hint
Common Mistakes
Using reserved keywords as function names
Using spaces or invalid characters in the name
2fill in blank
medium

Complete the code to await an async database query call.

FastAPI
result = [1] db.fetch_one(query)
Drag options to blanks, or click blank then click option'
Ayield
Bawait
Casync
Dcall
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use await causing coroutine objects instead of results
Using async instead of await
3fill in blank
hard

Fix the error in the async function declaration for a FastAPI route.

FastAPI
@app.get("/items")
async def [1]():
    items = await db.fetch_all("SELECT * FROM items")
    return items
Drag options to blanks, or click blank then click option'
Afetch_items
Bitems
Cget_items
Ditems_list
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic names like 'items' that can be confusing
Using names that don't match the route purpose
4fill in blank
hard

Fill both blanks to create a dictionary comprehension filtering async query results.

FastAPI
filtered = {item['id']: item for item in results if item[1] [2] 10}
Drag options to blanks, or click blank then click option'
A["value"]
B>
C<
D['count']
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys that don't exist in items
Using wrong comparison operators
5fill in blank
hard

Fill all three blanks to build an async function that queries and returns filtered data.

FastAPI
async def [1]():
    query = "SELECT * FROM users WHERE age [2] 21"
    results = await db.[3](query)
    return results
Drag options to blanks, or click blank then click option'
Aget_adult_users
B>
Cfetch_all
Dget_users
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function names
Using incorrect SQL operators
Using sync methods instead of async