2017-02-10 50 views
1

我正在努力弄清楚如何在从数据库中读取数据后清除元组'数据'。在For循环中清除Python中的元组

的工艺流程:

每隔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()  
+0

我可能会误解。你可以简单地在函数的结尾处设置'data = None'吗? – Fagan

+0

但是'b.execute'返回不同的数据?是否有任何理由将数据定义为全局? –

回答

1

在你的代码,您要执行这两条线:

global data 

data = b.fetchall() 

综合起来,这两个语句应该覆盖之前在data变量中的任何内容。

我会指出,这个函数不会出现在需要一个全局变量 - 你可以,也可能应该使用一个局部变量。

无论如何,我不认为问题是有一些神秘的剩余数据,除非b.fetchall()对象被定义为这样做。 (例如,如果查询中有错误,或者查询没有返回任何匹配,那么它是如何传达的?如果它提出或返回一个值,而您忽略的值,可能fetchall可能会返回陈旧的数据,因为您应该检查值,而不是调用fetchall ...)

我建议你看看如何executefetchall一起工作,并看看你的循环。我看到b.executeu.executeupdate.commit。这好像你有很多不同的数据库连接。也许你会。或者,也许你复制/粘贴的代码,你真的应该这样做:

u.execute(...) 
u.commit() 
+0

我对数据库连接/游标很蠢。问题已解决。 谢谢! –