Error giving python Mysql Insert

Asked

Viewed 103 times

0

I have the table in my bank:

create table tweets(
    ID_Tweet int not null auto_increment,
    ID_Candidato int not null,
    Conteudo_Tweet varchar(470) not null,
    constraint PK_tweets_ID primary key (ID_Tweet),
    constraint FK_tweets_IdCandidato foreign key (ID_Candidato) references candidatos (ID_Candidato)
);

In which I am trying to store the following content:

João Doria (@jdoriajr), on Oct 31, 2018, tweeted:

"Guys, I just got a visit from the governor-elect of Rio Big South, @Eduardoleite_. I’m glad to see such a young man competent and prepared in charge of its State. I thank the kindness of the visit. São Paulo and Rio Grande do Sul will accelerate together through the Brazil! #Accelerate"

Reply: 65 Retweet: 129 Like: 1,6 one thousand

To skip the lines I’m giving \n and I’m also putting \ before double quotes.

I’m trying to insert it this way:

    conexao = pymysql.connect(host='localhost', user='root', passwd='password', db='eleicoes', use_unicode = True, charset = 'utf8mb4', autocommit = True)
    cursorUrl = conexao.cursor()
    cursorUrl.execute(f'insert into tweets (ID_Candidato, Conteudo_Tweet) values ({1}, "{tweet}")') # tweet é a string com o conteúdo citado anteriormente
    cursorUrl.close()
    conexao.close()

However, I always get the same mistake:

Programmingerror: (1064, "You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'Personal, I just received the visit of the Governor elected by Rio Grande do Sul, ' at line 3")

When I try to include the same content directly through Mysql Workbench, I get it without problems. Everything leads me to believe that it has something to do with the special characters @ and _, but I’m not sure. What I’m doing wrong?

2 answers

1


How is it possible to check for the error message, more specifically on near 'Pessoal, recebi, the problem is not in the cited characters, but in the quotation marks of the text.

In this link there is a better example of how to do this kind of Insert: https://stackoverflow.com/a/12902951/1256062

sql = "insert into tweets (ID_Candidato, Conteudo_Tweet) values (%s, %s)"
cursorUrl.execute(sql, (idCandidato, tweet))

0

It seems to me you’re defining the value of Candidate incorrectly. Instead of {1} you shouldn’t have something like {id_candidato}? Kind of:

id_candidato = <ID do candidato aqui>
cursorUrl.execute(f'insert into tweets (ID_Candidato, Conteudo_Tweet) values ({id_candidato}, "{tweet}")')
  • In practice it’s equal to me putting the variable, I put the number 1 fixed intentionally because I’m referring to a specific candidate.

  • 1

    If you want to refer to the candidate of ID 1, you should not put the skulls {}. It’s just like this cursorUrl.execute(f'insert into tweets (ID_Candidato, Conteudo_Tweet) values (1, "{tweet}")')

Browser other questions tagged

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