Genere una excepción si la consulta sqlite está vacía

I have a method that do a sql-query and return a value. If empty it raise an exception. The code works as expected but it seems like there's a more clean way to do it? Or is it?

def get_song(self, number):
    db_cursor.execute("select * from songs where Id = ?", number)
    song = db_cursor.fetchone()
    if song: 
        return song
        raise ValueError

preguntado el 08 de noviembre de 11 a las 13:11

1 Respuestas

As the True statement doesn't improve your code (you just want to be assured it is not False) you could preserve some lines doing so:

if song != True:
    raise ValueError


if song == Null:
    raise ValueError

respondido 08 nov., 11:17

But, the True statement is returning a variable. Or am I missing something? - Niclas Nilsson

The variable is already assigned one line before, the return doesn't change the original content in it. - jonathan.hepp

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.