SQL SELECT的CS50财务历史记录问题

时间:2019-03-13 13:25:23

标签: sql cs50

我不明白为什么我不能从TABLE投资组合中选择“交易”列。我使用以下语法创建了一个表:

CREATE TABLE 'portfolio' ('transaction' integer primary key autoincrement, 
'datetime' datetime, user_id bigint, 'symbol' varchar(5), 'price' numeric(8, 
2), 'shares' integer, 'total' numeric(8, 2));

该交易记录了买卖订单的连续计数。当我在SELECT语句中有事务时,它给我一个错误,请参见下文:

RuntimeError: near "transaction": syntax error [SQL: 'SELECT transaction, 
datetime, symbol, shares, price FROM portfolio WHERE user_id = 2'] 
(Background on this error at: http://sqlalche.me/e/e3q8)

如果我不在python代码中包含事务,则一切正常,该表将显示在网页上。是什么让我无法选择交易?在代码中,我包含了事务。

@app.route("/history")
@login_required
def history():
    """Show history of transactions"""
    #create table
    history = db.execute("SELECT transaction, datetime, symbol, shares, price FROM portfolio WHERE user_id = :user_id", user_id = session["user_id"])

    history_info = []
    for info in history:
        transaction = info["transaction"]
        datetime = info["datetime"]
        symbol = info["symbol"]
        shares = info["shares"]
        price = info["price"]
        total = abs(shares * price)
        history_info.append({"transaction":transaction, "datetime":datetime, "symbol":symbol, "shares":shares, "price":price, "total":total})

    return render_template("history.html", history_info = history_info)

以下html将显示在网页上。目前,我已取消交易。

{% extends "layout.html" %}

{% block title %}
    History
{% endblock %}

{% block main %}
    <table class="table table-bordered">
        <thead>
            <th>Purchase Date/Time</th>
            <th>Symbol</th>
            <th>Shares</th>
            <th>Price</th>
            <th>Total</th>
        </thead>
        <tbody>
            {% for stock in history_info %}
                <tr>
                    <td>{{ stock.datetime }}</td>
                    <td>{{ stock.symbol }}</td>
                    <td>{{ stock.shares }}</td>
                    <td>{{ stock.price }}</td>
                    <td>{{ stock.total }}</td>
                </tr>
            {% endfor %}
        </tbody>
   </table>
{% endblock %}

1 个答案:

答案 0 :(得分:2)

已解决。...

transaction是保留字。

尝试将其放在方括号[transaction]中,看看是否适合您。