Hot answers tagged

3

From the C$50Finance spec Hints re the execute method: If str is a SELECT, then execute returns a list of zero or more dict objects, inside of which are keys and values representing a table’s fields and cells, respectively.


2

Edited original answer. You have your INSERT INTO syntax incorrect. Syntax should be: INSERT INTO tablename (Table Col1, Table Col2, etc.) VALUES (? marks for variables, static items enclosed in single quotes)", variables now separated by commas); So for your code above, the syntax would be (for static value of Symbol): query("INSERT INTO Portfolio (ID,...


2

'ON DUPLICATE KEY UPDATE' is MySQL syntax. But is this really what you need to do? Remember from the spec: Add one or more new tables to finance.db via which to keep track of the purchase. Store enough information so that you know who bought what at what price and when. In order to keep the required level of detail, each buy transaction must be one row ...


2

From the flask doc (Rendering Templates): To render a template you can use the render_template() method. All you have to do is provide the name of the template and the variables you want to pass to the template engine as keyword arguments. Notice the plurals. You can send multiple variables to render_template like render_template("buying.html", ...


2

Ok, I may be wrong about this and I can delete my answer so it stays unanswered if I am. I think the spec is a little murky referenced by Dino on the question linked above. Dino interpreted it to mean that each buy transaction needs a separate date/time, but I'd argue that it could also mean generally what was the users last transaction date with that ...


2

it seems like when I select the cash value from my SQL table, it is producing a list Quite true. From the Hints section of the spec: If str is a SELECT, then execute returns a list of zero or more dict objects, inside of which are keys and values representing a table’s fields and cells, respectively.


2

transaction is a sqlite reserved word. You'll need to escape the column name with quotes (eg 'transaction') or create the table again and rename the column.


2

I think the line if price * shares > cash: might be problematic on multiple stages. Here, price would be the float, and shares the sequence. shares is probably a string and needs conversion to integer first (also reject it if it contains any non-digit) cash is a list of dicts, most likely containing exactly one dict with one value. The value you want ...


2

According to the error message, this is an Operational Error. Your error message says: (Background on this error at: http://sqlalche.me/e/e3q8) That link point to a paragraph describing the Operational error. That might mean that something went wrong during some earlier database call. I think you should add some error checking after all your database ...


2

I can see a couple of things that you may consider changing and also most of your code is correct,so just few things to add on! The code where you are inserting values after checking that cash > stock in the else part as below : else: session["user_id"]=db.execute("INSERT INTO portfolio (stock, shares, balance) ...


2

buy should not contain a session["user_id"] =, you would overwrite the user id of the current session that way. The only place for that is login. That might be what causes the error message, you assign None or some number to that variable, but probably not a valid user ID. You don't use id in SQL statement, you should add user_id to the list of columns and ...


2

So my first question is, where does it come from? It's either bootstrap or the browser. check50 does not use the front end to make the API calls. Therefore any limits put on the html, while GREAT for the user, might not satisfy the spec, as evidenced by this problem. Keep the "check" in the html, it's good user interface (IMO). Add the necessary ...


1

Have you looked at all of the error or other messages returned? The portfolio table has 6 fields, all of which are NOT NULL and don't appear to have defaults, but the insert and update sql code only inserts 3 fields. I'll wager that the inserts/updates are being rejected because the remaining 3 fields aren't being supplied. If this answers your question, ...


1

The error is caused by the SQL command to INSERT failing because the table 'portfolio' doesn't exist. Create the table and then it should work. :)


1

I would expect you to have an autoincrementing id field that is the primary key (so each transaction has an id) and then a user_id field that holds the user_id for the transaction (that is not unique). This way your table can handle multiple users. Your insert is failing because you are trying to set the primary key.


1

I have a number of questions that may answer your issue. Does it still work if you create a new user and "buy" a new stock? Does it work for an old user and a new stock that nobody has bought? Does it work for a new user and an old stock that someone else has bought? Is the user making an additional purchase of stock that they already own? What are the ...


1

From your error message: line 143, in buy name=info["name"], ... \ KeyError: 'name' The issue appears to be the 'KeyError' 'name': name=info["name"] Does your dictionary 'info' have a key called name?


1

The problem is here db.execute=("UPDATE users SET cash= cash -:purchase WHERE id=:id", id = session["user_id"]). You are binding :purchase but you never set it to anything.


1

Ok, I found mistake - after WHERE if there is few conditions there have to be an AND between them


1

Just a small typo here :name;. Easy to miss, hard to find. (; is the sql delimiter, so "it" thinks you are trying to run 2 sqls).


1

You need to use the Post/Redirect/Get design pattern. By doing return render_template("buydisplay.html", rows = rows, cash = cashi), you are "staying" in the buy form with method "POST", so a refresh will do just as you have described. To prevent the problem, you will need to return a redirect. Something like return redirect(url_for("index")), similar to ...


1

I solved it using this link Challenge inserting into database from buy() as a resource. The problem was with the placement of my quotes I changed db.execute("INSERT INTO transactions (user_id, symbol, stock_price, no_of_shares, total_cost) VALUES (:user_id, :symbol, :stock_price, :no_of_shares, :total_cost), user_id=user_id, symbol=symbol, ...


1

The error from this sql INSERT INTO users ('transaction') VALUES(:cash) (quotes or not) is because there is no column named transaction in the users table. Now is the perfect time to add a table for transactions, but don't call it transaction, it's just a pain in the neck. (Generally speaking, in every language, avoid using reserved words to name things.) ...


1

@dinocodersaurus's link :) flash ("Bought") return redirect(url_for("index"))


1

I vote sophisticated :) The problem is :transaction_date, or lack thereof.


1

Here is the documentation for sqlite SELECT. The '.' represents the delimiter in a schema-name.table-name.column-name result-column expression.


1

Enclose the column name within square brackets . i.e [users.id] Just a suggestion, avoid using '.' character in sql attribute names.


1

And I can't see '666666' in my command console. Assuming you mean the browser console, that is not where you should be looking. Since application.py is running on the server, any print messages will appear on the server, ie in your flask run terminal, not in the browser console. That output can be quite "noisy"; think about making your debug message stand ...


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)


1

See if this rings any bells.... INSERT INTO portfolio (id, symbol, shares) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE ... If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


Only top voted, non community-wiki answers of a minimum length are eligible