【问题标题】:Error while displaying data from SQLite3 using Flask使用 Flask 显示来自 SQLite3 的数据时出错
【发布时间】:2021-06-04 13:59:43
【问题描述】:

我正在创建一个使用 API 购买股票的应用。目标是显示一个投资组合,其中购买的股票显示当前价格。几天来我一直在研究索引功能,但我经常遇到错误。我将不胜感激任何提示和帮助。不用说,我是一个新手和自学者。 我有以下错误:文件“/home/ubuntu/finance/helpers.py”,第 34 行,在 decorated_function 返回 f(*args, **kwargs) 文件“/home/ubuntu/finance/application.py”,第 57 行,在索引中 股=股票[0][“股”] KeyError:“共享”

enter code here@app.route("/")
@login_required
def index():
    """Show portfolio of stocks"""
    user_id=session["user_id"]
    stocks= db.execute("SELECT symbol, sum(shares) as total_cost FROM stocks WHERE user_id=:user_id GROUP BY symbol", user_id=user_id)
    grand_total=0
    stocks_data=[]

    for stock_data in stocks:

        symbol=stocks[0]["symbol"]
        shares=stocks[0]["shares"]
        name=stocks[0]["name"]
        price=lookup(symbol)
        total_cost=shares*price["price"]
        grand_total+=total_cost
        stocks_data.append(stock_data)



    return render_template("index.html", stocks=stocks, stocks_data=stocks_data )

HTML 在这里

{% extends "layout.html" %}

{% block title %}
    Index
{% endblock %}

{% block main %}
    <h2>Portfolio</h2>
                <table>
                    <thead>
                        <tr>
                            <th>Symbol</th>
                            <th>Name</th>
                            <th>Shares</th>
                            <th>Price</th>
                            <th>Total Cost</th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- TODO: Loop through the database to display all transactions and the balance -->
                        {% for stock_data in stocks_data %}
                           <tr>
                               <td>{{stock_data.symbol}}
                               <td>{{stock_data.name}}</td>
                               <td>{{stock_data.shares}}</td>
                               <td>{{(stock_data["price"])|usd}}</td>
                               <td>{{(stock_data["total_cost"])|usd}}</td>

                           </tr>

                        {% endfor %}
                           <tr>
                               <th>CASH</th>
                               <td>

                               </td>
                               <td></td>
                               <td></td>
                               <td></td>
                               <td>{{cash}}</td>


                           </tr>
                           <tr>
                               <th>Grand Total</th>
                               <td>

                               </td>
                               <td></td>
                               <td></td>
                               <td></td>
                               <td>{{grand_total|usd}}</td>


                           </tr>

                    </tbody>

                </table>
            </div>

        </div>
{% endblock %}

【问题讨论】:

    标签: python sqlite flask


    【解决方案1】:

    sum(shares) 在这里是total_cost 的别名stocks= db.execute("SELECT symbol, sum(shares) as total_cost.....。没有名为shares 的键;它被命名为total_cost。要么在sql中更改别名,要么在此处更改键名shares=stocks[0]["shares"]

    【讨论】:

    • 非常感谢。我已将其更改为stocks= db.execute("SELECT symbol, name, sum(shares) as share, price, total_cost FROM stock WHERE user_id=:user_id GROUP BY symbol", user_id=user_id)
    • 不客气。对于your reading pleasure
    猜你喜欢
    • 2021-06-07
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多