2

I have implemented the buy function so that I can successfully add purchased stocks to a new database, but upon working on the index function I think my database is structured incorrectly.

Right now my database has a unique transaction number, id, price, symbol, and shares. My problem is when trying to pull the data by id to create the table in index I end up with multiples of the same symbol. Is there a better way to structure my database in buy so that I don't run into this problem?

1
  • Did you create a unique index that includes BOTH the ID and SYMBOL columns? This will prevent multiple rows for the same ID and symbol. Of course, you'll have to have code to update a row when it already exists instead of adding a new row. (I'm assuming that this is the portfolio table and not the transaction table.) – Cliff B Jan 27 '17 at 22:16
3

I ended up figuring it out. In the buy function I checked if there was already a symbol under the user's id. If there was, I just used the UPDATE command. If there wasn't I used the INSERT INTO command. I kept getting a syntax error before, but I worked it out.

For the index function I looped through all the holdings and made a list of dictionaries which I passed it into index.html. Hopefully this helps someone else.

2
  • Thank you! I am doing this pset now and have the exact same problem. The walkthroughs are not very clear on this aspect... – Cam Feb 14 '17 at 5:05
  • I'm having the same problem, but can't figure it out. I have multiple instances of only one share that shows in html table, I don't see any other transactions from the same USER, just multiple instances of first purchase... I went through your answer but I don't understand it, can you please be more specific? – bobby_pin Apr 11 '17 at 6:57

You must log in to answer this question.

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