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 = []
Desmond
  • 59
  • 11
Ahijit
  • 113
  • 2
  • 15
  • An example of the returned data would be beneficial. – tgikal Jul 06 '18 at 15:46
  • I myself am not very sure how the data is returned that's why I'm having this problem – Ahijit Jul 06 '18 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. – tgikal Jul 06 '18 at 15:50
  • 2
    Are you just asking about iterating through the results? Could you use `for row in rows:`? – bzier Jul 06 '18 at 16:01
  • Not able to understand your question. Can't `join` work for you in this case? – mad_ Jul 06 '18 at 16:34

1 Answers1

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}]
Mike
  • 2,514
  • 2
  • 23
  • 37