0

Although I already did Pset7 finance I wanted to learn something new with python and flask, I find it really interesting but I have the feeling on many occasions that the syntax is little documented, specifically the requests to the database, if I request a value with SELECT using db.execute it seems that I am being returned a dictionary:

diner =  db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])

where diner is {'cash': 10000}

then if I want to access the actual value of the current cash, I have to do something like:

available = diner[0]['cash']

then available = 10000 my question is, should not that be enough available = diner['cash']? I do not understand why the first bracket [0] (well I'm a little new in python)

1 Answer 1

1

A select statement can always potentially return multiple results, either expected, or because there may be a problem with the data (corrupt database, entry error, etc.), so the process always returns an array, even if it is an array of only one element.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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