Skip to main content
incorporated author's own comment
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Is nesting MySQL queries with try-except blocks within python3 a correct way to write code?error handling

I have this function that I have written, that gets passed a row of data from a mysql function. The function then uses a try-except block to get some a list of values depending on a value within the row of data.

It then loops through this data and completes another try-except block within the first try-except block's else command.

My question hereIn fact, a lot of my code is styled this way. Some blocks go on for so long, or other blocks start once one has finished. Is this good practice of writing code? Or should I refactor my code and move the second try-except block into another separate function?

def select_uploaded_series(row):    
        try:
            select_series_name = """SELECT track_ser_series 
            FROM track_series 
            WHERE track_ser_id = %s"""
            dbDets[1].execute(select_series_name,(row['track_id']),)
            series_names = dbDets[1].fetchall()
        except Exception as e:
            raise(e)
        else:
            
            names = []
            
            for series in series_names:
                try:
                    select_series_name_query = """SELECT title 
                    FROM series 
                    WHERE categoryId = %s"""
                    dbDets[1].execute(
                            select_series_name_query,
                            (series['track_ser_series']),)
                    track_series_names = dbDets[1].fetchall()
                except Exception as e:
                    raise(e)
                else:
                    
                    for t_names in track_series_names:
                        names.append(t_names['title'])
                        
            return(names)

Is nesting try-except blocks within python3 a correct way to write code?

I have this function that I have written, that gets passed a row of data from a mysql function. The function then uses a try-except block to get some a list of values depending on a value within the row of data.

It then loops through this data and completes another try-except block within the first try-except block's else command.

My question here is this good practice of writing code? Or should I refactor my code and move the second try-except block into another separate function?

def select_uploaded_series(row):    
        try:
            select_series_name = """SELECT track_ser_series 
            FROM track_series 
            WHERE track_ser_id = %s"""
            dbDets[1].execute(select_series_name,(row['track_id']),)
            series_names = dbDets[1].fetchall()
        except Exception as e:
            raise(e)
        else:
            
            names = []
            
            for series in series_names:
                try:
                    select_series_name_query = """SELECT title 
                    FROM series 
                    WHERE categoryId = %s"""
                    dbDets[1].execute(
                            select_series_name_query,
                            (series['track_ser_series']),)
                    track_series_names = dbDets[1].fetchall()
                except Exception as e:
                    raise(e)
                else:
                    
                    for t_names in track_series_names:
                        names.append(t_names['title'])
                        
            return(names)

MySQL queries with try-except error handling

I have this function that I have written, that gets passed a row of data from a mysql function. The function then uses a try-except block to get some a list of values depending on a value within the row of data.

It then loops through this data and completes another try-except block within the first try-except block's else command.

In fact, a lot of my code is styled this way. Some blocks go on for so long, or other blocks start once one has finished. Is this good practice of writing code? Or should I refactor my code and move the second try-except block into another separate function?

def select_uploaded_series(row):    
        try:
            select_series_name = """SELECT track_ser_series 
            FROM track_series 
            WHERE track_ser_id = %s"""
            dbDets[1].execute(select_series_name,(row['track_id']),)
            series_names = dbDets[1].fetchall()
        except Exception as e:
            raise(e)
        else:
            
            names = []
            
            for series in series_names:
                try:
                    select_series_name_query = """SELECT title 
                    FROM series 
                    WHERE categoryId = %s"""
                    dbDets[1].execute(
                            select_series_name_query,
                            (series['track_ser_series']),)
                    track_series_names = dbDets[1].fetchall()
                except Exception as e:
                    raise(e)
                else:
                    
                    for t_names in track_series_names:
                        names.append(t_names['title'])
                        
            return(names)
Source Link
mp252
  • 507
  • 7
  • 15

Is nesting try-except blocks within python3 a correct way to write code?

I have this function that I have written, that gets passed a row of data from a mysql function. The function then uses a try-except block to get some a list of values depending on a value within the row of data.

It then loops through this data and completes another try-except block within the first try-except block's else command.

My question here is this good practice of writing code? Or should I refactor my code and move the second try-except block into another separate function?

def select_uploaded_series(row):    
        try:
            select_series_name = """SELECT track_ser_series 
            FROM track_series 
            WHERE track_ser_id = %s"""
            dbDets[1].execute(select_series_name,(row['track_id']),)
            series_names = dbDets[1].fetchall()
        except Exception as e:
            raise(e)
        else:
            
            names = []
            
            for series in series_names:
                try:
                    select_series_name_query = """SELECT title 
                    FROM series 
                    WHERE categoryId = %s"""
                    dbDets[1].execute(
                            select_series_name_query,
                            (series['track_ser_series']),)
                    track_series_names = dbDets[1].fetchall()
                except Exception as e:
                    raise(e)
                else:
                    
                    for t_names in track_series_names:
                        names.append(t_names['title'])
                        
            return(names)