Complete the code to import the async database library.
from databases import [1]
The Database class is imported from the databases library to create an async database connection.
Complete the code to create a database connection with the URL.
database = Database([1])The connection string for PostgreSQL uses the format postgresql://user:pass@host/dbname. This is the correct URL for the Database constructor.
Fix the error in the async function to connect to the database.
async def connect_db(): await database.[1]()
The Database class uses the connect() method to open the connection asynchronously.
Fill both blanks to create a query and fetch all rows asynchronously.
query = "SELECT * FROM users WHERE id = [1]" rows = await database.[2](query)
The query uses a named parameter :user_id for safe substitution. The fetch_all method retrieves all matching rows asynchronously.
Fill all three blanks to insert a new user asynchronously and get the inserted ID.
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)
The query uses named parameters :name and :email for safe insertion. Include RETURNING id (for PostgreSQL) and use the fetch_one method to retrieve the inserted record's ID asynchronously. The result is a dict like {'id': 1}.