В настоящее время я нахожусь в программе на python, где я получаю удовольствие, получая твиты, связанные с темой, которую я отдаю, и затем сохраняю их в базе данных sqlite. Вставка идет очень хорошо за исключением того, что время от времени у меня появляется эта ошибка:
sqlite3.OperationalError: near "random word": syntax error
Я не могу понять причину ошибки, и каждый раз, когда я искал в Интернете, она никогда не была мне полезна.
Код ниже:
import sqlite3
import tweepy
import csv
import pandas as pd
import json
import codecs
import preprocessor as p
import textblob.exceptions
from sqlite3 import Error
from googletrans import Translator
from textblob import TextBlob
import nltk
import time
from nltk.tokenize import word_tokenize
def create_connection(db_file):
try:
conn = sqlite3.connect(db_file)
print("Connecté a la base de données ! ")
except Error as e:
print(e)
finally:
conn.close()
def create_table(conn, create_table_sql):
"""
:param conn: Connection object
:param create_table_sql: a CREATE TABLE statement
:return:
"""
try:
c = conn.cursor()
c.execute(create_table_sql)
print("Table créée")
except Error as e:
print(e)
def insert_table(conn, sql):
try:
c = conn.cursor()
c.executemany(sql)
print("Requete acceptée")
except Error as e:
print(e)
def main():
database = "C:\\Users\TheoLC\AppData\Local\Programs\Python\Python37\lib\sqlite3\pythonsqlite.db"
try:
conn = sqlite3.connect(database)
print("Connecté a la base de données ! ")
except Error as e:
print(e)
auth = tweepy.OAuthHandler("lol")
auth.set_access_token("lol")
api = tweepy.API(auth)
translator = Translator()
search_word = input("subject ? \n")
search_word = TextBlob(search_word)
search_word_finnish = translator.translate(str(search_word), dest='fi')
search_word_french = translator.translate(str(search_word), dest='fr')
print("Mot en finnois : " + str(search_word_finnish.text) + " \n")
print("Mot en français : " + str(search_word_french.text) + " \n")
searched_tweets = []
taille = input("nb de tweets ?")
new_tweets_en = api.search(search_word, count=int(taille)/3)
new_tweets_fi = api.search(search_word_finnish.text, count=int(taille)/3)
new_tweets_fr = api.search(search_word_french.text, count=int(taille)/3)
print("j'ai trouver ", len(new_tweets_en), "tweets en anglais")
print("j'ai trouver ", len(new_tweets_fi), "tweets en finnois")
print("j'ai trouver ", len(new_tweets_fr), "tweets en français")
if not new_tweets_en and not new_tweets_fr and not new_tweets_fi:
print("pas de tweets trouves")
new_tweets = new_tweets_en + new_tweets_fr # + new_tweets_fi
searched_tweets.extend(new_tweets)
c = conn.cursor()
c.execute("DELETE FROM tweets")
conn.commit()
for tweet in searched_tweets:
tweet.text = tweet.text.encode('unicode-escape').decode('utf-8')
# c = conn.cursor()
c.execute("INSERT INTO tweets (id_tweet, username , content) VALUES (\"%s\", \"%s\", \"%s\");" %(tweet.id, tweet.author.screen_name, tweet.text))
conn.commit()
print("Requete acceptée")
if __name__ == "__main__":
main()
Спасибо тем, у кого хватит терпения прочитать lol