Home > Software engineering >  sqlalchemy.exc.ProgrammingError Flask SQLAlchemy cannot read data from my table
sqlalchemy.exc.ProgrammingError Flask SQLAlchemy cannot read data from my table

Time:01-04

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'.

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. So basically my table has 3 columns - sno (autoincremented and primarykey), employee_name and employee_status. I want to read the whole table in my app, whose main.py is as follows -

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 as follows -

<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>

my config.json goes here

{
    "params":
    {
        "sitehr_password": "asdf",
        "gmail-user":"[email protected]",
        "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

my vscode error in terminal is as follows-

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

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. Thanks in advance !!

CodePudding user response:

Your class is data_reader is faulty.

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 data_reader(db.Model):
    __tablename__ = 'kolkata'
    sno = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
  •  Tags:  
  • Related