In the pset7 Walkthrough (buy): db.execute("SELECT cash FROM users WHERE id = 1")

Where does this "id = 1" come from ?

This, makes better sense to me: db.execute("SELECT cash from users WHERE id = :id", id = session['user_id']).

With the former, I got an "index error", with the latter, it works fine.

Thanks in advance!


It looks like you've implemented it correctly. In the walkthrough, she is using 1 as an example, with the assumptions that it really is user 1 and that the user exists in the database. It also looks like she is leaving it to the student to determine how to get the user ID when it is unknown, along with all other values.

You could be getting an error for id = 1 for any number of reasons. My best guess is that you've set up the database to sequentially assign user ID numbers to users and have been testing for a little while. In the process, perhaps you deleted all users or at least user 1, but didn't reset the autocounter to 1 for the table (it's held in the db's metadata.) If you didn't reset the counter, it will just keep going from wherever it was. A quick look at the user table will answer this.

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

  • Indeed, that's exactly what happened! Thanks a lot! – Malintzin Jun 11 '18 at 4:56

You must log in to answer this question.

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