【问题标题】:(sqlite3.OperationalError) table transactions has no column - Python(sqlite3.OperationalError) 表事务没有列 - Python
【发布时间】:2017-12-13 13:21:14
【问题描述】:

我正在参加一个在线课程,该课程要求我使用 Python 和 sqlite 构建一个股票交易网站。我收到以下错误:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) table 
transactions has no column named purchase [SQL: "INSERT INTO 
transactions (username, symbol, price, quantity, purchase, timestamp) 
VALUES ('Ari', 'ZG', 40.39, 5, 201.95, '2017-12-13 03:58:52.905863');"]

如果您查看下面用于更新用户交易历史的代码,您会看到我正在将购买变量添加到 SQL 查询中,该查询也作为我的数据库中的一列。

from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session, url_for
from flask_session import Session
from passlib.apps import custom_app_context as pwd_context
from tempfile import mkdtemp
from datetime import datetime

from helpers import *

# configure application
app = Flask(__name__)

# ensure responses aren't cached
if app.config["DEBUG"]:
    @app.after_request
    def after_request(response):
        response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
        response.headers["Expires"] = 0
        response.headers["Pragma"] = "no-cache"
        return response

# custom filter
app.jinja_env.filters["usd"] = usd

# configure session to use filesystem (instead of signed cookies)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

# configure CS50 Library to use SQLite database
db = SQL("sqlite:///finance.db")

@app.route("/")
@login_required
def index():
    return apology("TODO")

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
    """Buy shares of stock."""

    # if user reached route via POST (as by submitting a form via POST)
    if request.method == "GET":
        return render_template("buy.html")
    else:
        # ensure user entered a symbol and shares
        symbol = request.form.get("symbol")
        shares = request.form.get("shares")
        if not symbol or not shares:
            return apology("Must input stock ticker and shares")

        # check to make sure stock is real
        stock = lookup(request.form.get("symbol"))
        if not stock:
            return apology("Stock does not exist")

        # make sure user inputs valid number of shares
        shares = int(shares)
        if shares <= 0:
            return apology("Shares must be positive integer")

        # select user's cash balance
        rows = db.execute("SELECT * FROM users WHERE id = :id", id=session["user_id"])
        cash = float(rows[0]["cash"])

        # total cost of purchase
        purchase = float(stock["price"]*float(shares))

        # does user have enough cash?
        if cash < purchase:
            return apology("You don't have enough money")

        # update user cash
        db.execute("UPDATE users SET cash = cash - :purchase WHERE id = :id;", \
                    purchase=purchase, id=session["user_id"])

        # update user's transaction history
        db.execute("INSERT INTO transactions (username, symbol, price, quantity, purchase, timestamp) \
                    VALUES (:username, :symbol, :price, :quantity, :purchase, :timestamp);", \
                    username=rows[0]["username"], symbol=stock["symbol"], price=float(stock["price"]), \
                    quantity=shares, purchase=purchase, timestamp=str(datetime.now()))

        # return to index
        return redirect(url_for("index"))

这是我的事务数据库结构的图像:

SQL transactions structure

你能帮我弄清楚我做错了什么吗?

提前致谢!

【问题讨论】:

    标签: python sqlite


    【解决方案1】:

    我刚刚删除了我的 sqlite 数据库中的事务表并重新实现了它。现在它起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2015-07-30
      • 2016-02-20
      • 1970-01-01
      • 2017-06-19
      相关资源
      最近更新 更多