menu

Questions & Answers

Assign each value using different loops in python

Hi I got a loop where I get different parameters (age, date, role, etc.), and I got an array where I get these values. How can I assign each value to eache parameter. I'll add some code below

I tried this but obviosly didn't work

parameters = Parametros.objects.filter(job_id=job.id)
        if request.method == 'POST':    
            for parameter in parameters:
                #Update parameters
                params = request.POST.getlist('parameter')
                for i in range(len(params)):
                    cursor.execute("UPDATE Jobs_parametros SET parameter_value='" + params[i] + "' WHERE parameter_name='" + parameter.parameter_name + "' AND job_id=" + str(job.id))

Here we can see "parameters" where I'm assigning parameter.parameter.name for the different names. After that we can see the array where I get the values called "params". I want to assign each value I get from params to the name of the parameter to execute the code showed in the last line

The result that I got with the code showed is each parameter name with the last value I added. The values are updated but only take the last value in my form and didn't assigned my values like the date and age for eache parameter name

enter image description here

Comments:
2023-01-25 00:55:05
Previous iteration of this question: stackoverflow.com/questions/75225084/…
2023-01-25 00:55:05
Never construct SQL queries with string operations if you don't want to be a victim of SQL injection. Use placeholders and the second parameter of execute.
Answers(0) :