2017-02-10 48 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

我對數據庫連接/遊標很蠢。問題已解決。 謝謝! –