In: Computer Science
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
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 :)