def get_tablatures(self, artist_id, song_id): ''' Return list of tablatures to specified song. If parameters are left empty return all tablatures. If song parameter is left empty return all tablatures by artist. Return "None" if song, artist or tablatures are not found. ''' keys_on = 'PRAGMA foreign_keys = ON' if song_id == '' and artist_id == '': query = 'SELECT * FROM tablatures' elif song_id == '': query = 'SELECT * FROM tablatures WHERE artist_id = ?' pvalue = (artist_id, ) elif artist_id == '': query = 'SELECT * FROM tablatures WHERE song_id = ?' pvalue = (song_id, ) else: query = 'SELECT * FROM tablatures WHERE artist_id = ? AND song_id = ?' pvalue = ( artist_id, song_id, ) #artist_id = None #connects (and creates if necessary) to the database. gets a connection object con = sqlite3.connect(self.database_name) with con: con.row_factory = sqlite3.Row cur = con.cursor() cur.execute(keys_on) #execute the statement if song_id == '' and artist_id == '': cur.execute(query) else: cur.execute(query, pvalue) #get results rows = cur.fetchall() if rows == []: return None tablatures = [] for row in rows: tablatures.append(TablatureModel.create(row)) return tablatures
def get_tablatures(self, artist_id, song_id): """ Return list of tablatures to specified song. If parameters are left empty return all tablatures. If song parameter is left empty return all tablatures by artist. Return "None" if song, artist or tablatures are not found. """ keys_on = "PRAGMA foreign_keys = ON" if song_id == "" and artist_id == "": query = "SELECT * FROM tablatures" elif song_id == "": query = "SELECT * FROM tablatures WHERE artist_id = ?" pvalue = (artist_id,) elif artist_id == "": query = "SELECT * FROM tablatures WHERE song_id = ?" pvalue = (song_id,) else: query = "SELECT * FROM tablatures WHERE artist_id = ? AND song_id = ?" pvalue = (artist_id, song_id) # artist_id = None # connects (and creates if necessary) to the database. gets a connection object con = sqlite3.connect(self.database_name) with con: con.row_factory = sqlite3.Row cur = con.cursor() cur.execute(keys_on) # execute the statement if song_id == "" and artist_id == "": cur.execute(query) else: cur.execute(query, pvalue) # get results rows = cur.fetchall() if rows == []: return None tablatures = [] for row in rows: tablatures.append(TablatureModel.create(row)) return tablatures
def get_tablature(self, tablature_id): """ Return a tablature. Return "None" if tablature was not found. """ keys_on = "PRAGMA foreign_keys = ON" query = "SELECT * FROM tablatures WHERE tablature_id = ?" pvalue = (tablature_id,) # connects (and creates if necessary) to the database. gets a connection object con = sqlite3.connect(self.database_name) with con: con.row_factory = sqlite3.Row cur = con.cursor() cur.execute(keys_on) # execute the statement cur.execute(query, pvalue) # just one result possible row = cur.fetchone() if row is None: return None return TablatureModel.create(row)
def get_tablature(self, tablature_id): ''' Return a tablature. Return "None" if tablature was not found. ''' keys_on = 'PRAGMA foreign_keys = ON' query = 'SELECT * FROM tablatures WHERE tablature_id = ?' pvalue = (tablature_id, ) #connects (and creates if necessary) to the database. gets a connection object con = sqlite3.connect(self.database_name) with con: con.row_factory = sqlite3.Row cur = con.cursor() cur.execute(keys_on) #execute the statement cur.execute(query, pvalue) #just one result possible row = cur.fetchone() if row is None: return None return TablatureModel.create(row)