[英]sqlalchemy.exc.ProgrammingError Flask SQLAlchemy cannot read data from my table
Guys I am working on a website project.伙计们,我正在做一个网站项目。 On which I need help on reading a whole table and displaying it on my website page 'index.html'.我需要帮助阅读整个表格并将其显示在我的网站页面“index.html”上。
Requirement: I have a table 'kolkata' on localhost phpmyadmin using SQLAlchemy of flask, I've searhed too much on google but can't find solution so I came here.要求:我在本地主机 phpmyadmin 上有一个表 'kolkata',使用 flask 的 SQLAlchemy,我在这里找到了太多的解决方案。 So basically my table has 3 columns - sno (autoincremented and primarykey), employee_name and employee_status.所以基本上我的表有 3 列 - sno(自动增量和主键)、employee_name 和employee_status。 I want to read the whole table in my app, whose main.py is as follows -我想在我的应用程序中阅读整个表格,其 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)
my index.html is empty我的 index.html 是空的
my config.json goes here我的 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"
}
}
when i run the code, i get an error like this - https://i.stack.imgur.com/7Yeop.png当我运行代码时,我收到这样的错误 - https://i.stack.imgur.com/7Yeop.png
my vscode error in terminal is as follows-我在终端中的 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)
a pic of my phpmyadmin database - https://i.stack.imgur.com/6oxVQ.png我的 phpmyadmin 数据库的图片 - https://i.stack.imgur.com/6oxVQ.png
I literally can't understand what is its meaning and what to do next, i wasted my 3 hours on it but nothing could be reached, now I am completely dependent on you people.我真的无法理解它的含义以及接下来要做什么,我浪费了我 3 个小时的时间,但什么也达不到,现在我完全依赖于你们。 Thanks in advance !!提前致谢 !!
Your class is data_reader
is faulty.您的 class 是data_reader
故障。
class data_reader(db.Model):
sno = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(30))
You need to add table name to this class:您需要将表名添加到此 class:
class data_reader(db.Model):
__tablename__ = 'kolkata'
sno = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(30))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.