25.8k views
5 votes
Write an application in the language of your choice to access the database using the created user in task # 1 with a function to run each query in tasks 4-8 with the user ID passed as a parameter. (10 points) Create functions to insert users and insert & delete favorites. Insert at least 3 users and 4 favorites for each user. You may use static data in the program to call these functions programmatically. Note that a user may only add existing bands to favorites list and function should return an error if they add a band not in the database. (You may add your favorite bands to the template provided, just remember to add matching genre/sub_genre and region/country.)

User Gazdagergo
by
8.3k points

1 Answer

5 votes

Step-by-step explanation:

# Function to access the database using the created user in task #1

def access_database(user_id):

# Connect to the database using the provided user ID

# Your code to establish a connection and authenticate the user

# Once connected, you can run the queries and perform operations using the user ID

run_query_4(user_id)

run_query_5(user_id)

run_query_6(user_id)

run_query_7(user_id)

run_query_8(user_id)

# Close the database connection

# Your code to close the connection

# Function to insert a user into the database

def insert_user(user_id, username):

# Check if the user with the given user ID already exists in the database

# Your code to query the database and check if the user exists

# If the user exists, return an error or handle accordingly

# Your code to handle the existing user case

# If the user doesn't exist, insert the user into the database

# Your code to insert the user with the provided user ID and username

# Function to insert a favorite band for a user

def insert_favorite(user_id, band_name):

# Check if the band exists in the database

# Your code to query the database and check if the band exists

# If the band doesn't exist, return an error or handle accordingly

# Your code to handle the non-existent band case

# If the band exists, insert it as a favorite for the user

# Your code to insert the favorite band for the user with the provided user ID

# Function to delete a favorite band for a user

def delete_favorite(user_id, band_name):

# Check if the band exists in the user's favorite list

# Your code to query the database and check if the band exists in the user's favorites

# If the band doesn't exist in the user's favorites, return an error or handle accordingly

# Your code to handle the non-existent favorite band case

# If the band exists in the user's favorites, delete it

# Your code to delete the favorite band for the user with the provided user ID

# Example usage

if __name__ == '__main__':

# Insert users

insert_user(1, "John")

insert_user(2, "Alice")

insert_user(3, "Bob")

# Insert favorites for each user

insert_favorite(1, "Metallica")

insert_favorite(1, "Iron Maiden")

insert_favorite(1, "Led Zeppelin")

insert_favorite(2, "AC/DC")

insert_favorite(2, "Queen")

insert_favorite(2, "Pink Floyd")

insert_favorite(3, "Nirvana")

insert_favorite(3, "The Beatles")

insert_favorite(3, "Radiohead")

# Delete a favorite band for a user

delete_favorite(1, "Iron Maiden")

# Access the database with user ID 1

access_database(1)

User Mr Jonny Wood
by
8.2k points

No related questions found