在从数据库中读取数据之后,我很难弄清楚如何清除元组“数据”。
流程:
每隔X分钟,我就打电话给batchUpdate
batchUpdate提取符合特定条件的记录
我们遍历那些执行更新的记录。
进程结束并等待下一次调用。
发行:
每次对函数batchUpdate的后续调用都不会产生新的数据。元组'data‘包含与以前相同的值。
简化的示例(每1秒只提取一条记录):
Update has started
((10938L, u"@anonymized @anonymized House of Cards will be nothing compared to the Drumpf's!And worst of all is that Americans chose him as president?", u'New York, USA'),)
Update has started
((10938L, u"@anonymized @anonymized House of Cards will be nothing compared to the Drumpf's!And worst of all is that Americans chose him as president?", u'New York, USA'),)
Update has started
((10938L, u"@anonymized @anonymized House of Cards will be nothing compared to the Drumpf's!And worst of all is that Americans chose him as president?", u'New York, USA'),)
Update has started
((10938L, u"@anonymized @anonymized House of Cards will be nothing compared to the Drumpf's!And worst of all is that Americans chose him as president?", u'New York, USA'),)代码:
class analyzeRecords():
def batchUpdate(self):
global data
#Select up to 1 record
b.execute(""" SELECT id, tweet,tweet_location_guess FROM rawTweets where compoundScore IS NULL LIMIT 0,1 """)
#Put that data into a tuple
data = b.fetchall()
print(data)
#print that update has started
print("Update has started")
for row in data:
idMatch = row[0]
cleanTweet = reduce(lambda x, y: x.replace(y, d[y]), sorted(d, key=lambda i: len(i), reverse=True), row[1].lower())
sentimentScores = analyzer.polarity_scores(cleanTweet)
overallScore = sentimentScores["compound"]
u.execute("""UPDATE rawTweets SET tweet=%s, compoundScore=%s where id=%s""",
(cleanTweet, overallScore, idMatch))
update.commit()
l = task.LoopingCall(analyzeRecords().batchUpdate)
l.start(timeout) # call every sixty seconds
reactor.run() 发布于 2017-02-10 16:43:38
在您的代码中,您将执行以下两行:
global data
data = b.fetchall()综合起来,这两个语句应该覆盖以前data变量中的任何内容。
我要指出的是,这个函数似乎不需要全局变量--您可以而且很可能应该为此使用一个局部变量。
无论如何,我不认为问题是存在一些神秘的剩余数据,除非定义了b.fetchall()对象。(例如,如果查询中有错误,或者查询没有返回匹配项,那么它是如何传递的?如果它引发或返回一个您忽略的值,也许fetchall会返回陈旧的数据,因为您应该检查该值,而不是调用fetchall.)
我建议您看看execute和fetchall是如何一起工作的,还可以看看您的for循环。我看到了b.execute,u.execute和update.commit。似乎您有许多不同的数据库连接。也许你知道。或者,您可以复制/粘贴代码,您确实应该这样做:
u.execute(...)
u.commit()https://stackoverflow.com/questions/42164147
复制相似问题