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?
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.
– Daniel Santos
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}")')
– Diogo Aguiar