vertica-python icon indicating copy to clipboard operation
vertica-python copied to clipboard

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

Open singhpratibha58 opened this issue 5 years ago • 2 comments

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

singhpratibha58 avatar Mar 27 '20 07:03 singhpratibha58

Please provide the version info for those packages you installed. Thanks.

sitingren avatar Mar 30 '20 09:03 sitingren

I have used 0.10.2 driver version.

singhpratibha58 avatar Mar 31 '20 08:03 singhpratibha58