Next, my question is more about logic anyway ..
I have a python script in which I use psycopg2 to perform searches in the database. But I need to do this within a for.
I have full notion that doing a search within a for can affect and much in code performance, besides getting something 'ugly'.
I already thought of other solutions like for example to bring a general query and work with 1 loop inside another to replace the select but I believe that it would give me a high processing the same way ....
When I am faced with a situation of which the best exit to avoid a select to each for loop?
for busca_filas in rows_sip:
if busca_filas[1] == 'QUEUE':
dict_filas.update({"fisico": busca_filas[2]})
dict_filas.update({"virtual": busca_filas[4]})
cursor.execute("SELECT * FROM queue_members where queue_name = '" + busca_filas[2] + "';")
busca_ramais_filas = cursor.fetchall()
for item_ramais_filas in busca_ramais_filas:
dict_detalhe_filas.update({'nome_fila': item_ramais_filas[0]})
dict_detalhe_filas.update({'ramal': item_ramais_filas[3]})
dict_detalhe_filas.update({'pausado': item_ramais_filas[4]})
list_detalhe_filas.append(dict_detalhe_filas)
dict_detalhe_filas = {}
dict_filas.update({'ramais': list_detalhe_filas})
list_filas.append(dict_filas)
list_detalhe_filas = []
dict_filas = {}
I created this sample code just for you to understand how my code is.