1

I'm stuck again! I'm on pset7 'buy' and think I'm almost done - it's deducting the stock price from the 'users' table fine, but when I try to insert the data into the 'transactions' table I get an error msg "RuntimeError: (sqlite3.OperationalError) no such column: user [SQL: "INSERT INTO 'transactions' (users_id, symbol, number, price, date, total_cost) VALUES (user, symbol, quantity, price, datetime('now'), total)"]

I don't understand why it's saying there's no 'user' column when 'user' is in the VALUES brackets - it's a variable that I declared earlier in the code so I could use it here - not a column...I've fiddled around with the order but I just get a different error saying I can't use the datetime bit (it calls it an undeclared var)....I'm not 100% sure the problem isn't with the datetime bit as I found how to use it right (I think!) online but there's no indication in the cs50 docs about it...

Any help would be so appreciated - Thank you!!

Here's my 'buy' code: http://pastebin.com/HiPmy952

1

You need to use bound parameters in the VALUES clause (with the exception of datetime('now')), otherwise names will be interpreted as string literals. Something like
db.execute("INSERT INTO transctions (users_id,datetime) VALUES (:user, datetime('now'))", user = user)

5
  • Thanks, DinoCoderSaurus (again!)...but do you mean like this: db.execute("INSERT INTO transactions (user, symbol, number, price, total_cost, datetime('now')) VALUES (?,?,?,?,?)", (user, symbol, quantity, price, total) because then I get 'invalid syntax'.... – CallyB Mar 18 '17 at 17:54
  • The '?' notation is last year's syntax (MySQL). Abridged example added to answer. – DinoCoderSaurus Mar 18 '17 at 18:06
  • Aha!!! I was so confused over the diff syntax - finally I realise why there are diff ones - some of them are old!! I've put it in but am still getting 'no such column: now'. Do I need to import something to use the datetime(now) feature? I've set the field as datetime type and called it 'date' - is that ok? thanks again....! – CallyB Mar 18 '17 at 18:27
  • I did a quick repro, and datetime('now') worked. Make sure you enclose "now" in quotes? (NB datetime('now') is sqlite syntax, not python) – DinoCoderSaurus Mar 18 '17 at 18:47
  • arggg! So sorry - my bad - typo...a stray quote - it's working...THANK YOU!!! (again!) You really are saving my sanity on this course!!! :-) – CallyB Mar 18 '17 at 19:52

You must log in to answer this question.

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