I'm trying to understand a function that could help me in the SELL part of the pset7, but so far, I'm failing...

I want to select from my database every shares own by the user, grouping them by name while adding them. My request looks like this :

total_stock = db.execute("SELECT *, SUM(nb_of_shares) FROM portfolio WHERE username = :username AND stock_symbol = :stock_symbol", 
        username = user[0]["username"], 
        stock_symbol = stock_to_sell)

By passing this order onto phpLite, I can see that it casts a result, one line including a new row SUM(nb_of_shares)

From this, I would like to get the result of it as an integer, thing that I thought could work like this :


but it seems to be of a NoneType. I tried a lot of other options but none of them worked. Any idea if this is possible? Thanks in advance!


That syntax should work. I suspect that total_stock[0]["SUM(nb_of_shares)"] is 'None' because it did not find any rows that matched username AND stock_symbol. When you use the aggregate function (SUM), this query will always return 1 row. If it hasn't found any rows, all the values will be 'None'.

  • Ok... The mistake came from the fact that I requested the sell using lower case for the quote so it couldn't find it... ... ... I should probably erase this question, I'm quite sure now that it won't help anybody. Thank you for your answer!
    – Laurent_P
    Apr 18 '17 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .