Question

In: Computer Science

Your task in this exercise is to write a function called get_playlist_tracks(database_filename, which_playlist), which formulates a...

Your task in this exercise is to write a function called get_playlist_tracks(database_filename, which_playlist), which formulates a SELECT statement that returns track name, album title, genre name, artist name and track composer for all tracks in the database which are associated with a given playlist. This statement requires several nested INNER JOINs to pull together data from the "tracks", "albums", "genres", "artists", and "playlists" table. The default ordering of tracks should be used. The result of the function is a list of database records. The parameter which_playlist specifies a playlist in the database. You first have to check if the playlist is contained in the "playlists" table, if not an error message as indicated in the examples has to be printed and an empty list has to be returned.

Don't forget to close the database connection after you have finished accessing it!

For example:

Test Result
database_filename = 'chinook.db'
which_playlist = 'On-The-Go 1'
query_result = get_playlist(database_filename, which_playlist)

print(f"Playlist '{which_playlist}'")
for count, database_record in enumerate(query_result):
   print(f"{count:<2} Track Name: {database_record[0]}")
   print(f"   Album Title: {database_record[1]}")
   print(f"   Genre: {database_record[2]}")
   print(f"   Artist: {database_record[3]}")
   print(f"   Composer: {database_record[4]}")
Playlist 'On-The-Go 1'
0  Track Name: Now's The Time
   Album Title: The Essential Miles Davis [Disc 1]
   Genre: Jazz
   Artist: Miles Davis
   Composer: Miles Davis
database_filename = 'chinook.db'
which_playlist = 'Independent'
query_result = get_playlist(database_filename, which_playlist )

for database_record in query_result:
   print(database_record)
ERROR: Could not find playlist 'Independent' in database!

------------------------------------------------------------------------------------------------------------------------------------------------------------------

This is my code so far but dones't work. the output shows that there is no such table as track;

import sqlite3

def get_playlist(database_filename, which_playlist):
  
file_in = sqlite3.connect(database_filename)
curser_object = file_in.cursor()
curser_object.execute("""Select T.name, A.Title, G.name, A1.name, T.composer
From track T
Join Album A On A.AlbumId = T.AlbumId
Join Genre G On G.GenreId = T.GenreId
Join Artist A1
On A1.ArtistId = A.ArtistId
Join PlaylistTrack P2
On P2.TrackId = T.TrackId
Join Playlist P
On P.PlaylistId = p2.PlaylistId
Where P.name =?""",(which_playlist,))
  
ViewData = curser_object.fetchall()
DataTableCompAndClient([ViewData])
  
file_in.close()
return ViewData

Solutions

Expert Solution

I hope below code may help you

def get_playlist(database_filename, which_playlist):      
    with sqlite3.connect(database_filename) as db:
        cursor = db.cursor()
        cursor.execute("""Select T.name, A.Title, G.name, A1.name, T.composer
From Track T
Join Album A
On A.AlbumId = T.AlbumId
Join Genre G
On G.GenreId = T.GenreId
Join Artist A1
On A1.ArtistId = A.ArtistId
Join PlaylistTrack P2
On P2.TrackId = T.TrackId
Join Playlist P
On P.PlaylistId = p2.PlaylistId
Where P.name =?""",(which_playlist,))
        ViewData = cursor.fetchall()
        DataTableCompAndClient([ViewData])
    db.commit()
    return ViewData

---------------------------------------------

Please give me a UPVOTE. Thank you :)


Related Solutions

In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
Your Task: Write a calculator Program with following functions (lack of function will result in a...
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1 - Add 2 - Subtract 3 - Multiply 4 - Divide 5 - Raise X to the power Y 6 - Finds if a number is even or odd 0 - Quit...
Your Task: Write a calculator Program with following functions (lack of function will result in a...
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1 - Add 2 - Subtract 3 - Multiply 4 - Divide 5 - Raise X to the power Y 6 - Finds if a number is even or odd 0 - Quit...
Write a function called HowMany(), which counts the occurrences of the second argument which is a...
Write a function called HowMany(), which counts the occurrences of the second argument which is a single character in the first argument which is a string. This function should have 2 arguments, the first one is a string and the second argument is a character. For example, the following function : i = count("helloyoutwo", 'o'); would return i= 3. Test your function in a complete code. Language: c++
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a...
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a sinusoid wave having two components as f1 = 3kHz and f2 = 5kHz and then sample it with fs = 10kHz. Calculate its fft with zero frequency component in the middle. Plot it on a properly scaled w-axis. Specify if there is aliasing or not? If there is aliasing specify which component is casing the aliasing
Python using sqllite3 package In this exercise, your task is to inspect the given database, which...
Python using sqllite3 package In this exercise, your task is to inspect the given database, which is called 'chinook.db', as you can see from the testing code below in the example. We first would like to know how the logical schema of the database looks like, in order to work with it later in terms of reading from and writing to the database. Please also note that a software tool like "DB Browser for SQLite" can be used to inspect...
function exerciseOne(){ // Exercise One: In this exercise you will create a variable called 'aboutMe' //...
function exerciseOne(){ // Exercise One: In this exercise you will create a variable called 'aboutMe' // This variable should be assigned a new object // In this object create three key:value pairs // The keys should be: 'name', 'city', 'favoriteAnimal' // The values should be strings associated with the keys. // return the variable 'aboutMe' } function exerciseTwo(animal){ // Exercise Two: In this exercise you will be given an object called 'animal' // Create a new variable called 'animalName' //...
In this exercise, you will build a basic spell checker. Write a module called spell_checker.py. Your...
In this exercise, you will build a basic spell checker. Write a module called spell_checker.py. Your module should contain the following functions: word_correct(string) -> string word_correct() takes in a string consisting of one word. It searches for the line in aspell.txt containing that word and replaces it with the first word present in the file. If the word is not in the file, it returns the same word back. For example, word_correct("cookie") = "cookie" word_correct("acommadate") = "accommodate" word_correct("basically") = "basically"...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output of longestMorseCodeWords should be an array of the strings that were passed in, but ordered by the length of their Morse Code equivalent in descending order. If the length of Morse Code is equal, order the words alphabetically.For convenience, the full table for the 26 letters of the English alphabet is given below: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] let words = ["gin", "zen", "gig", "msg"] longestMorseCodeWords(words) The Morse...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT