sqlite3.Operationalerror: near " ": syntax error

Asked

Viewed 3,133 times

0

you know what that mistake 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"
  • In which line is making the mistake?

  • In the where idturma Missing close single quote '

  • here: c.execute("update classrooms set codigoturma = '" + self.codigoturma + "', periodo= '" + self.periodo + "', turmacodigodisciplina = '" + self.turmacodigodisciplina + "', turmacpfprofessor = '" + self.turmacpfprofessor + "', list_cpf_students = '" + self.list_cpf_students + "' Where idturma = " + self.idturma)

  • Try to see if that’s what I said in my last comment.

  • All right. I’ll look, thank you !!

  • c.execute("update classes set codigoturma = '" + self.codigoturma + "', periodo= '" + self.periodo + "', turmacodigodisciplina = '" + self.turmacodigodisciplina + "', turmacpfprofessor = '" + self.turmacpfprofessor + "', list_cpf_students = '" + self.list_cpf_students + "' Where idturma = '" + self.idturma) sqlite3.Operationalerror: unrecognized token: "'"

  • gave this error now.... : ( is hard.... self.c.execute("" Attributeerror: 'Class' Object has no attribute 'c'

  • your reply gave this error there :( .

Show 3 more comments

1 answer

1


I tried to reformat the part of your code that is giving error, to make it a little less confusing, it is just a syntax error related to quotes, so it 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 injection into the variables, and performs better, because once the interpolation is done by the database, the compiled query can be cached.

  • 1

    Perhaps it would be interesting for you to explain how a parameterized query and what is the structure of the parameters that can be specified in the SQL statement, perhaps AP does not know this :)

  • @true cat, I’ll edit the question by adding this information, I’m a little busy, but as soon as I can rewrite the answer ;)

  • I didn’t know which was the bad quotes. Only if it was the idturma and was a non-numerical variable

Browser other questions tagged

You are not signed in. Login or sign up in order to post.