1

我无法将数据插入到我的表中。我有一个传递给函数 getStockData 的股票列表。

我使用 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()
4

1 回答 1

2

我认为您忘记在关闭之前将数据提交到数据库。

尝试

conn.commit()
于 2012-05-15T05:59:52.140 回答