我正在努力弄清楚如何在从数据库中读取后清除元组'数据'。
流程:
每隔X分钟,我调用batchUpdate
batchUpdate拉入符合特定条件的记录
我们遍历那些执行更新的记录
流程结束并等待下一个电话
问题:
对函数batchUpdate的每次后续调用都不会产生新数据。元组'data'包含与之前相同的值。
简化示例(仅拉1条记录,每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()
答案 0 :(得分:1)
在您的代码中,您正在执行以下两行:
client.EnableSsl = false;
client.UseDefaultCredentials = false;
总之,这两个语句应该覆盖之前 global data
data = b.fetchall()
变量中的任何内容。
我会指出这个函数似乎不是需要一个全局变量 - 你可以而且可能应该只使用一个局部变量。
无论如何,我认为问题不在于存在一些神秘的剩余数据,除非data
对象被定义为这样做。 (例如,如果查询中存在错误,或者查询没有返回匹配项,那么它是如何进行通信的?如果它引发或返回您忽略的值,则b.fetchall()
可能会返回过时的数据因为你应该检查值而不是调用fetchall ...)
我建议您查看fetchall
和execute
如何协同工作,并查看您的for循环。我看到fetchall
和b.execute
以及u.execute
。这似乎有许多不同的数据库连接。也许你这样做。或许你复制/粘贴代码,你真的应该做的事情如下:
update.commit