【发布时间】:2017-03-10 02:47:34
【问题描述】:
我正在尝试创建一个存储推文的 SQLite 数据库。每天我调用 API 并获得 10 万条左右的推文进行查询。
鉴于 Twitter API 可以追溯到 7 天,并且某些推文的某些值必然会发生变化(转发量、收藏夹等),我需要一种方法来更新数据库中已经存在的每条推文.
这就是我将新推文添加到数据库的方式(其中 'parsed' 是字典列表):
# query to add each tweet to the database
for tweet in parsed:
c.execute("INSERT OR IGNORE INTO tweets VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
[tweet['id'],
tweet['url'],
tweet['created_at'],
tweet['hashtags'],
tweet['favorite_count'],
tweet['user_mentions'],
tweet['text'],
tweet['user_verified'],
tweet['user_following_count'],
tweet['retweet_count'],
tweet['user_name'],
tweet['user_id'],
tweet['user_screen_name'],
tweet['geo'],
tweet['lang'],
tweet['user_followers_count']]
)
这是我迄今为止更新推文的查询:
update_tweet_query = '''UPDATE tweets
SET url = ? ,
created_at = ? ,
hashtags = ? ,
favorite_count = ? ,
user_mentions = ? ,
text = ? ,
user_verified = ? ,
user_following_count = ? ,
retweet_count = ? ,
user_name = ? ,
user_id = ? ,
user_screen_name = ? ,
geo = ? ,
lang = ? ,
user_followers_count = ?
WHERE id = ?'''
但我不知道如何从这里开始......我是否走在正确的轨道上?有没有更好的方法来做到这一点?
提前致谢!
【问题讨论】: