使用Python 2.7将数据插入Sqlite3数据库

时间:2012-05-15 05:03:41

标签: python sqlite python-2.7

我在将数据插入表格时遇到问题。我有一个我传递给函数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()

1 个答案:

答案 0 :(得分:2)

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

尝试

conn.commit()