2012-05-15 177 views
1

我無法將數據插入到我的表中。我有一個我傳遞給函數getStockData的股票列表。數據沒有被插入到SQLite3數據庫使用Python 2.7

我使用for循環遍歷列表並獲取每個股票代碼的數據。最後,我把所有的信息都放到了字典裏。我最後一步是將數據插入表中。我在將字典中的數據插入到表中時不成功。

def getStockData(x): 

    nowdate = raw_input("What Is Todays Date?: ") 
    print "Todays list has %d stocks on it\n" % len(x) 

    for stock in x: 

     stockPrice = ystockquote.get_price(stock) 

     stockPriceChange = ystockquote.get_change(stock) 

     originalPrice = float(stockPrice) + (float(stockPriceChange) * -1) 

     changePercentage = (float(stockPriceChange)/originalPrice) * 100 

     stockDict = {'Date': nowdate, 'Ticker Symbol': stock, 'Closing Price': stockPrice, 
       'Price Change': stockPriceChange, 'Percentage Changed': changePercentage} 

     conn = db.connect('stocks.db') 
     cursor = conn.cursor() 
     cursor.execute('insert into losers values (?, ?, ?, ?, ?)', (stockDict['Date'], stockDict['Ticker Symbol'], stockDict['Price Change'], 
           stockDict['Percentage Changed'], stockDict['Closing Price'])) 

     conn.close() 

回答

2

我想你忘了在關閉之前將你的數據提交給你的數據庫。

嘗試

conn.commit() 
+0

這工作。謝謝! – TDNS