sqlite3.OperationalError: near "": syntax error

0

Do you know what this error means?

Error:

sqlite3.OperationalError: near “ ”: syntax error

Code:

def alterturma(self):

        bancoturmas = Bancoturmas()
        #try:

        c = bancoturmas.conexao.cursor()

        c.execute("update turmas set codigoturma = '" + self.codigoturma + "', periodo= '" + self.periodo + "', turmacodigodisciplina = '" + self.turmacodigodisciplina + "', turmacpfprofessor = '" + self.turmacpfprofessor + "', lista_cpf_alunos = '" + self.lista_cpf_alunos + "' where idturma = " + self.idturma)


        bancoturmas.conexao.commit()  
        c.close()

        return "Turma atualizada com sucesso!"
        #except:
            #return "Ocorreu um erro na alteração da turma"
    
asked by anonymous 05.08.2018 / 13:55

1 answer

1

I tried to reformat the part of your code that is giving error, to make it a little less confusing, it's just a syntax error related to the quotes, so you should solve:

self.c.execute("""
        UPDATE turmas
          codigoturma = ?,
          periodo = ?,
          turmacodigodisciplina = ?,
          turmacpfprofessor = ?,
          lista_cpf_alunos = ?,
        WHERE idturma = ?
        """, (self.codigoturma, self.periodo,self.turmacodigodisciplina,self.turmacpfprofessor,self.lista_cpf_alunos,self.idturma)) 

Note that this query is using parameterization (rather than simple string interpolation as in your example). This has several advantages: It frees you from having to put the right quotes, prevents SQL code from being injected into the variables, and performs better, because once the interpolation is done by the database, the compiled query can be cached.

    
05.08.2018 / 15:07