Error in sql-Insert command when passing a variable as parameter (python 3)

Asked

Viewed 355 times

1

I am trying to pass my "table" variable to the mysql command, but it is returning an error (of sql) during execution. What am I missing?

Here I created the table variable

table = input('Table name: ')

while(tabela not in tabelasCadastradas):
    tabela = input('Tabela não cadastrada. Digite novamente:'.upper())
print(('Menu -> 1- Adicionar questão').upper())
opcaoMenu = input('')
while(opcaoMenu != '1'):
    opcaoMenu = ('Opção inválida. Digite novamente: '.upper())
tema = input('Tema: '.upper())
enunciado = input('Enunciado: '.upper())

Here is giving error

crawler.execute('INSERT INTO "%s" (id, tema, enunciado) VALUES (id, "%s", "%s")'%(tabela, tema, enunciado))

Conn.close()

  • What is the error that appears?

  • Says I have a syntax error in mysql command.

  • 1

    pymysql.err.Programmingerror: (1064, 'You have an error in your SQL syntax;

  • When I do Crawler.execute('INSERT INTO" (id, theme, enunciation) VALUES (id, "%s", "%s")'%(theme, enunciation)), works perfectly. So I know it’s really something wrong when passing the table variable as parameter.

1 answer

-1

Resolved with: crawler.execute('INSERT INTO{}VALUES (id, \'{}\', \'{}\')'.format(tabela, tema, enunciado))

Browser other questions tagged

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