将 Flask 应用程序与 sqlite 数据库连接的问题

问题描述 投票:0回答:2

我在将 Flask Web 应用程序连接到 sqlite 数据库时遇到问题 请帮我解决这个问题。 我尝试执行 chatgpt 和 Gemini 聊天机器人给出的建议,但问题仍然存在!!

    sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user
[SQL: SELECT user.id AS user_id, user.username AS user_username, user.email AS user_email, user.image_file AS user_image_file, user.password AS user_password
FROM user
WHERE user.email = ?
LIMIT ? OFFSET ?]
[parameters: ('[email protected]', 1, 0)]
(Background on this error at: https://sqlalche.me/e/20/e3q8)

就是这个。我遇到错误的代码

from flask import Flask, render_template, url_for, flash, redirect
from form import RegistrationForm, LoginForm, BookForm, UploadBook, Contact, DeleteBook
from recomm import recom
from flask_sqlalchemy import SQLAlchemy
from PIL import Image
import os
import pandas as pd
import numpy as np
from flask_table import Table, Col

class Results(Table):
    id = Col('Id', show=False)
    title = Col('TOP RECOMMENDATIONS')

app=Flask(__name__)

SECRET_KEY = os.urandom(32)
app.config['SECRET_KEY'] = SECRET_KEY
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///site.db'
db=SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    password = db.Column(db.String(60), nullable=False)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}')"

posts = [
    {
    'author' : 'Eshita Jain',
    'title' : 'International Institute of Information Technology'
    }
]

@app.route("/")
@app.route("/home")
def home():
    return render_template('home.html',posts=posts,title="Home")


@app.route("/about")
def about():
    return render_template('about.html',posts=posts,title="About")


@app.route("/register", methods=['GET','POST'])
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        flash(f'Account Created for { form.username.data } !', 'success')
        return redirect(url_for('home'))
    return render_template('register.html',title='Register',form=form)


@app.route("/login", methods=['GET','POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        if form.email.data == '[email protected]' and form.password.data == 'password':
            flash(f'You have been logged in !', 'success')
            return redirect(url_for('home'))
        else:
            flash(f'Login Unsuccessful. Please check login details again.', 'danger')
    return render_template('login.html',title='Login',form=form)


@app.route("/recommender",methods=['GET','POST'])
def recommender():
    form=BookForm()
    df=pd.read_csv('Book.csv')
    if form.validate_on_submit():
        if form.bookname.data in list(df['Title']):
            flash(f'Here are the following recommendations for you', 'success')
            isbn=[]
            year=[]
            publisher=[]
            final_list=[]
            book=form.bookname.data
            output, index = recom(book)
            for i in index:
                isbn.append(df["ISBN"][i-1])
                year.append(df["Year"][i-1])
                publisher.append(df["Publisher"][i-1])
            for i in range(len(index)):
                temp=[]
                temp.append(output[i])
                temp.append(isbn[i])
                temp.append(year[i])
                temp.append(publisher[i])
                final_list.append(temp)
        
            return render_template('recommender.html',title='Recommender',form=form,final=final_list)
        else:
            flash(f'Name not clearly mentioned or does not exist in the database. Please try again.', 'danger')
            return redirect(url_for('recommender'))
    return render_template('recommender.html',title='Recommender',form=form)

@app.route("/uploadbook",methods=['GET','POST'])
def uploadbook():
    form=UploadBook()
    df=pd.read_csv('Book.csv')
    if form.validate_on_submit():
        flash(f'Book Uploaded Succesfully', 'success')
        return redirect(url_for('home'))
    return render_template('uploadbook.html',title='Upload Book',form=form)


@app.route("/contact",methods=['GET','POST'])
def contact():
    form=Contact()
    if form.validate_on_submit():
        flash(f'Query Submission Succesful', 'success')
        return redirect(url_for('contact'))
    return render_template('contact.html',title='Upload Book',form=form)


@app.route("/deletebook",methods=['GET','POST'])
def deletebook():
    form=DeleteBook()
    if form.validate_on_submit():
        flash(f'Book is Deleted', 'success')
        return redirect(url_for('home'))
    return render_template('deletebook.html',title='Delete Book',form=form)


@app.route("/account")
def account():
    image_file = url_for('static', filename='profile_pics/' + current_user.image_file)
    return render_template('account.html', title='Account',image_file=image_file, form=form)


if __name__ == '__main__':
    app.run(debug=True)

这是我想运行的图书推荐系统代码,但面临类似上述错误的问题。请帮我解决这个问题

python database sqlite flask
2个回答
0
投票

我认为您需要先使用

db.create_all()
创建数据库表。您可以在
if __name__ == '__main__'
下或在单独的脚本中执行此操作,如下所示:

from app import db, User
db.create_all()

-1
投票

您必须将 User.username 类更改为 User.user

© www.soinside.com 2019 - 2024. All rights reserved.