Skip to content

[Flask integration] Vertica-python integration with flask is not working for update table #364

Description

@singhpratibha58

User is able to create table, insert , delete records but update query is not working . It is not showing any error message and just refreshing the page.Here is the script:

import os
from flask import Flask
from flask import render_template
from flask import request
from flask import redirect
from flask_sqlalchemy import SQLAlchemy
 
app = Flask(__name__)
app.config.update(
    SECRET_KEY='',
    SQLALCHEMY_DATABASE_URI='vertica+vertica_python://<dbuser>:<dbpassword>@<dbmachineip>:<dbport>/<dbname>',
    SQLALCHEMY_TRACK_MODIFICATIONS= False
    )
 
db = SQLAlchemy(app)
 
class Employee(db.Model):
    emp_name = db.Column(db.String(80), unique=True, nullable=False, primary_key=True)
 
    def __repr__(self):
        return "<Employeename: {}>".format(self.emp_name)
 
 
@app.route("/", methods=["GET", "POST"])
def home():
    employees = None
    if request.form:
        try:
            employee = Employee(emp_name=request.form.get("emp_name"))
            db.session.add(employee)
            db.session.commit()
        except Exception as e:
            print("Failed to show employee name ")
            print(e)
    employees = Employee.query.all()
    return render_template("home.html", employees=employees)
 
 
@app.route("/update", methods=["POST"])
try:
        new_name = request.form.get("newname")
        old_name = request.form.get("oldname")
        employees = Employee.query.filter_by(emp_name=old_name).first()
        employees.emp_name = new_name
        db.session.commit()
    except Exception as e:
        print("Couldn't update employee name")
        print(e)
    return redirect("/")
 
 
@app.route("/delete", methods=["POST"])
def delete():
    emp_name = request.form.get("delname")
    employees = Employee.query.filter_by(emp_name=emp_name).first()
    db.session.delete(employees)
    db.session.commit()
    return redirect("/")
 
 
if __name__ == "__main__":
    db.create_all()
    app.run(host='ServerHostIP')

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions