1

I'm having trouble inserting data into my table. I have a list of stocks that I pass to the function getStockData.

I use a for loop to iterate through the list and get the data for each ticker symbol. At the end I put all the information into a dictionary. My final step is to insert the data into a table. I've been unsuccessful at inserting the data in the dictionary into my table.

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 Answer 1

2

I think you forget to commit your data to your DB before close.

Try

conn.commit()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.