我目前正在使用python程序,在其中很有趣的是检索与我给出的主题相关的推文,然后将其存储在sqlite数据库中。插入进行得很好,除了有时会发生以下错误:
sqlite3.OperationalError: near "random word": syntax error
我无法理解错误的根源,并且每次搜索Internet时,对我来说都是无用的。
以下代码:
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()
感谢那些有耐心阅读大声笑的人
答案 0 :(得分:1)
问题是这一行:
c.execute("INSERT INTO tweets (id_tweet, username , content) VALUES (\"%s\", \"%s\", \"%s\");" % (tweet.id, tweet.author.screen_name, tweet.text))
sqlite的参数替换字符为?
。使用'%s'
插值意味着值可能无法正确转义,因此您的SQL语句可能已损坏,或者您可能会受到SQL注入攻击。
正确的代码是:
c.execute("INSERT INTO tweets (id_tweet, username , content) VALUES (?, ?, ?);",
(tweet.id, tweet.author.screen_name, tweet.text))
问号?
用作参数替换字符,而execute
方法处理替换(无%
运算符)。