0

The stock returns the stocks owned by the user. I want to pass a function lookup() on each stock returned to get the price of the stock. How will I do it since the query returns multiple rows?

rows = "SELECT * FROM :p1 ORDER BY id"
db.execute(rows,{'p1':str(session['user_id'])})
rows = db.fetchall()
stock = rows[0]['symbol']
share = rows[0]['shares']
price = []
5
  • An example of the returned data would be beneficial. Commented Jul 6, 2018 at 15:46
  • I myself am not very sure how the data is returned that's why I'm having this problem Commented Jul 6, 2018 at 15:48
  • 1
    I know a few of these types of things return data in very odd manners, and it sounds like the data may contain somewhat personal information, you might need to do some digging and figure out the structure, or at least remove personal data and post the structure. Commented Jul 6, 2018 at 15:50
  • 2
    Are you just asking about iterating through the results? Could you use for row in rows:? Commented Jul 6, 2018 at 16:01
  • Not able to understand your question. Can't join work for you in this case? Commented Jul 6, 2018 at 16:34

1 Answer 1

1

Hard to say if this is what you're looking for but sounds like you're looking for a way to search a list of dictionaries.

>>> rows = [{'symbol': 'APPL', 'shares':1000},{'symbol': 'GE', 'shares': 2000},{'symbol': 'TWTR', 'shares': 5000}]
>>> def lookup(rows, stock):
...    results = [row for row in rows if row["symbol"] == stock]
...    return results
... 
>>> lookup(rows, 'APPL')
[{'symbol': 'APPL', 'shares': 1000}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.