sqlalchemy.exc.ProgrammingError Flask SQLAlchemy 无法从我的 table 中读取数据

sqlalchemy.exc.ProgrammingError Flask SQLAlchemy cannot read data from my table

伙计们,我正在做一个网站项目。我需要帮助阅读整个 table 并将其显示在我的网站页面 'index.html'.

要求: 我在 localhost phpmyadmin 上有一个 table 'kolkata' 使用 flask 的 SQLAlchemy,我在 google 上搜索了太多但找不到解决方案所以我来到这里。所以基本上我的 table 有 3 列 - sno(自动增量和主键),employee_name 和 employee_status。我想在我的应用程序中阅读整个 table,其 main.py 如下 -

from flask import Flask, render_template, request, session
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail
import json
from datetime import datetime
from sqlalchemy import select
import sqlalchemy

with open('config.json', 'r') as c:
    params = json.load(c)["params"]

local_server = True
app = Flask(__name__)
app.secret_key = 'super-secret-key'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config.update(
    MAIL_SERVER = 'smtp.gmail.com',
    MAIL_PORT = '465',
    MAIL_USE_SSL = True,
    MAIL_USERNAME = params['gmail-user'],
    MAIL_PASSWORD=  params['gmail-password']
)
mail = Mail(app)
if(local_server):
    app.config['SQLALCHEMY_DATABASE_URI'] = params['local_uri']
else:
    app.config['SQLALCHEMY_DATABASE_URI'] = params['prod_uri']

db = SQLAlchemy(app)

class data_reader(db.Model):
    sno = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))

@app.route("/", methods=["GET", "POST"])
def home():
    if (session['user_pass'] == params['sitehr_password']):
        data = db.session.query(data_reader)
        print(data)

        #here i need to add the code which u will give me

        return render_template('index.html', employeedata=data)

    if request.method=='POST':
        username = request.form.get('uname')
        userpass = request.form.get('pass')
        usersite = request.form.get('select_site')
        if (userpass == params['sitehr_password']):
            #set the session variable
            session['user'] = username
            session['user_pass'] = userpass
            session['user_site'] = usersite
            return render_template('index.html')
            
    return render_template("login.html")

app.run(debug=True)

我的index.html如下-

<html>
    <head>
        <title>
            Employee Status Manager
        </title>
    </head>
    <body>
        <table>
            <th>Serial No.</th>
            <th>Employee Name</th>
            {% for row in employeedata %}
            <tr>
                <td> {{row.sno}} <td>
                <td> {{row.employee_name}} <td>
            <tr>    
            {% endfor %}
        </table>
        {{employeedata}}
    </body>
</html>

我的 config.json 在这里

{
    "params":
    {
        "sitehr_password": "asdf",
        "gmail-user":"parvanshusharma12@gmail.com",
        "gmail-password":"******",
        "local_uri": "mysql://root:@localhost/employeestatus",
        "prod_uri":"mysql://root:@localhost/employeestatus"
    }
}

当我 运行 代码时,我得到这样的错误 - https://i.stack.imgur.com/7Yeop.png

我的vscode终端报错如下-

sqlalchemy.exc.ProgrammingError
sqlalchemy.exc.ProgrammingError: (MySQLdb._exceptions.ProgrammingError) (1146, "Table 'employeestatus.data_reader' doesn't exist")
[SQL: SELECT data_reader.sno AS data_reader_sno, data_reader.name AS data_reader_name 
FROM data_reader]
(Background on this error at: https://sqlalche.me/e/14/f405)

我的 phpmyadmin 数据库的图片 - https://i.stack.imgur.com/6oxVQ.png

我真的无法理解它的含义和下一步该怎么做,我在上面浪费了 3 个小时,但什么也达不到,现在我完全依赖你们这些人。提前致谢!!

你的class是data_reader有问题。

class data_reader(db.Model):
    sno = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))

您需要将 table 名称添加到此 class:

class data_reader(db.Model):
    __tablename__ = 'kolkata'
    sno = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))