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

Programming Exercise 1 Write a function that will have a list as an input, the task...
Programming Exercise 1 Write a function that will have a list as an input, the task of the function is to check if all the elements in the list are unique,( i.e. no repetition of any value has occurred in the list), then the function returns true otherwise it returns false. Your program should include a main method that call the method to test it. Algorithm Analysis For the function you implemented in part 1, please calculate T(n), which represents...
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
- Write a function with a parameter called A (which is between 1 and 10) and...
- Write a function with a parameter called A (which is between 1 and 10) and prints the multiplication table for 1 to A. • Note: Do not need to draw the horizontal and vertical lines. • Example for A = 10.
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++
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...
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
Write in Python. come up with a programming task for which you will write your own...
Write in Python. come up with a programming task for which you will write your own program. It should be a purposeful and non-trivial program, bigger than any programming assignment. But it should not be something too ambitious that you may not end up finishing by the deadline. The program must not be something available on the Internet or in a book. The program must satisfy the following five requirements: It must define and appropriately use at least one class....
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' //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT