phpmyadmin SQLAlchemy无法从我的表中读取数据

bqf10yzr  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(165)

伙计们,我正在做一个网站项目。我需要帮助阅读整个表格,并将其显示在我的网站页面“index.html”。
要求:我有一个表'kolkata'在localhost phpmyadmin使用SQLAlchemy的 flask ,我已经搜索了太多的谷歌,但不能找到解决方案,所以我来到这里.所以基本上我的表有3列- sno(自动增量和主键),雇员_名称和雇员_状态.我想在我的应用程序中读取整个表,其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个小时,但什么也不能达到,现在我完全依赖于你们的人。提前感谢!!

c3frrgcw

c3frrgcw1#

你的类是data_reader是错误的。

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

您需要将表名添加到此类:

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

相关问题