import mysql.connector connector = mysql.connector.connect( host="localhost", user="root", database="balalaika", ) cursor = connector.cursor() def get_song(id, name): if id is None: id = "%" if name is None: name = "%" cursor.execute( """ select * from song where id like %(id)s and name like %(name)s; """, { 'id': id, 'name': name, } ) return cursor.fetchall() def get_filtered_songs(song, album, artist): if artist is None: artist = "%" if album is None: album = "%" cursor.execute( """ select song.name, album.name, artist.name from song inner join album on song.album_id=album.id inner join artist on album.artist_id=artist.id where artist.name like %(artist)s and album.name like %(album)s; """, { 'artist': artist, 'album': album, 'song': song, } ) return cursor.fetchall()