简体   繁体   English

尝试根据值删除 sqlite 中的行时出现内部服务器错误

[英]Internal server error while trying to delete rows in sqlite based on their value

[ [在此处输入图像描述

I am trying to create an app for buying and selling stocks.我正在尝试创建一个用于买卖股票的应用程序。 If I sell all stocks from the same company, I want to delete the row instead of displaying 0 shares.如果我出售同一家公司的所有股票,我想删除该行而不是显示 0 股。 I am not sure how to delete the row based on its value.我不确定如何根据其值删除该行。

It gives me error incomplete input here:它在这里给了我错误不完整的输入:

db.execute("INSERT INTO stocks (user_id, symbol, name, shares, price, total_cost, transaction_type) VALUES(:user_id, :symbol, :name, :shares, :price, :total_cost, :transaction_type", user_id=user_id, name=name, symbol=symbol, shares= -1*shares, price=price, total_cost=total_cost, transaction_type=transaction_type)

This line of code was working before I added the delete condition.在我添加删除条件之前,这行代码正在工作。

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():

    """Sell shares of stock"""
    #Access the current user
    user_id= session["user_id"]

    if request.method =="POST":
        if not request.form.get("symbol") or not request.form.get("shares"):
            return apology("Enter a valid symbol or number of shares")

        #Define data
        symbol=request.form.get("symbol")
        shares=int(request.form.get("shares"))
        stock=lookup(symbol)
        price=stock.get("price")
        total_cost=int(shares)*stock["price"]
        name=stock.get("name")
        transaction_type="sale"

        if stock is None:
            return apology("Enter a valid symbol")

        #Access existing data in DB

        rows= db.execute("SELECT symbol, sum(shares) as shares FROM stocks WHERE user_id=:user_id GROUP BY symbol", user_id=user_id)


        #Validate if the current user owns the shares they are trying to sell
        for row in rows:
            if row["symbol"]==symbol:
                if shares > row["shares"]:
                    return apology("Enter a valid number of shares")
                if row["shares"]-shares==0:
                    db.execute("DELETE FROM stocks WHERE user_id = :user_id AND symbol = :symbol",
                          symbol=symbol, user_id=session["user_id"])


        user=db.execute("SELECT cash FROM users WHERE id=:user_id", user_id=user_id)
        new_cash=user[0]["cash"]+total_cost
        #Add transaction to the db
        #Update DB cash of the user

        db.execute ("UPDATE users SET cash=:new_cash WHERE id=:id", new_cash=new_cash, id=user_id)
        db.execute("INSERT INTO stocks (user_id, symbol, name, shares, price, total_cost, transaction_type) VALUES(:user_id, :symbol, :name, :shares, :price, :total_cost, :transaction_type", user_id=user_id, name=name, symbol=symbol, shares= -1*shares, price=price, total_cost=total_cost, transaction_type=transaction_type)

        return redirect("/")

    else:
        share_symbols=[]
        symbs = db.execute("SELECT symbol FROM stocks WHERE user_id=:user_id GROUP BY symbol",
        user_id=user_id)
        for symb in symbs:
            share_symbols.append(symb)
        return render_template("sell.html", share_symbols=share_symbols)

One problem is order of execution.一个问题是执行顺序。 This这个

db.execute("DELETE FROM stocks WHERE user_id = :user_id AND symbol = :symbol",
                          symbol=symbol, user_id=session["user_id"])

deletes all stocks rows if shares will become 0.如果 share变为 0,则删除所有股票行。

Subsequently,随后,

db.execute("INSERT INTO stocks (user_id, symbol, name, shares, price, total_cost, transaction_type) VALUES(:user_id, :symbol, :name, :shares, :price, :total_cost, :transaction_type", user_id=user_id, name=name, symbol=symbol, shares= -1*shares, price=price, total_cost=total_cost, transaction_type=transaction_type)

inserts a row with negative shares.插入负份额的行。

It looks like the stocks table keeps one row per transaction, not one row per stock.看起来股票表每笔交易保留一行,而不是每只股票一行。 Which would be useful for the history route.这对历史路线很有用。 Don't delete any rows.不要删除任何行。

One solution would be a modification, probably in index.一种解决方案是修改,可能在 index.html 中。 Assuming a SELECT with sum(shares), add a HAVING clause to filter out rows where sum(shares) is 0.假设 SELECT 带有 sum(shares),添加HAVING 子句以过滤掉 sum(shares) 为 0 的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM