0
0
FastAPIframework~10 mins

Async database with databases library 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 import the async database library.

FastAPI
from databases import [1]
Drag options to blanks, or click blank then click option'
Adatabase
BDatabase
CDatabases
Ddb
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'database' instead of 'Database'.
Trying to import a non-existent module name.
2fill in blank
medium

Complete the code to create a database connection with the URL.

FastAPI
database = Database([1])
Drag options to blanks, or click blank then click option'
A'postgresql://user:pass@localhost/dbname'
B'mysql://user:pass@localhost/dbname'
C'mongodb://localhost:27017'
D'sqlite:///test.db'
Attempts:
3 left
💡 Hint
Common Mistakes
Using SQLite URL when expecting PostgreSQL.
Using MongoDB URL which is not supported by this library.
3fill in blank
hard

Fix the error in the async function to connect to the database.

FastAPI
async def connect_db():
    await database.[1]()
Drag options to blanks, or click blank then click option'
Aopen
Bstart
Cconnect
Dconnect_db
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like 'start' or 'open'.
Calling a method that does not exist on the database object.
4fill in blank
hard

Fill both blanks to create a query and fetch all rows asynchronously.

FastAPI
query = "SELECT * FROM users WHERE id = [1]"
rows = await database.[2](query)
Drag options to blanks, or click blank then click option'
A:user_id
Bfetch_all
Cexecute
D?user_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using '?' instead of ':user_id' for named parameters.
Using 'execute' which runs the query but does not fetch results.
5fill in blank
hard

Fill all three blanks to insert a new user asynchronously and get the inserted ID.

FastAPI
query = "INSERT INTO users(name, email) VALUES ([1], [2]) RETURNING id"
values = {"name": user_name, "email": user_email}
user_id = await database.[3](query=query, values=values)
Drag options to blanks, or click blank then click option'
A:name
B:email
Cexecute
Dfetch_one
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'execute' which runs the insert but does not return the ID for PostgreSQL.
Using positional parameters instead of named parameters.