Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot create a database with SqlAlchemy (sqlalchemy.exc.OperationalError)

I’m learning sqlAlchemy and I want to establish a connection and create a database to insert data. I have an error message that I don’t understand.
According to the documentation, the error may come from the database itself. Has anyone ever encountered this problem?

  File "C:\Users\A.NAITSAIDI\AppData\Local\Programs\Python\Python310\lib\site- 
  packages\psycopg2\__init__.py", line 122, in connect
  conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) 
  (Background on this error at: https://sqlalche.me/e/14/e3q8)

My code :

    from flask import Flask, render_template, request
    from flask_sqlalchemy import SQLAlchemy
    
    
    app = Flask(__name__)
    #configurer la connexion a la bdd
    app.config["SQLALCHEMY_DATABASE_URI"] = 'postgresql://postgres:password@localhost:5432/data_collector'
    
    
   
    db= SQLAlchemy(app)


class Data(db.Model):
    __tablename__ = "data"
    id=db.Column(db.Integer, primary_key=True)
    email_=db.Column(db.String(120), unique=True)
    height_=db.Column(db.Integer)

    def __init__(self, email_, height_):
        self.email_=email_
        self.height_=height_

@app.route("/")
def index():
    return render_template("index.html")


@app.route("/success", methods=['POST'])
def success():

    if request.method == 'POST':
        email = request.form["email_name"]
        height = request.form["height_name"]
        print(email)
        print(height)
        return render_template("success.html")


if __name__ == "__main__":

    app.debug == True
    app.run()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Seems like database data_collector is not created on your machine. After creating the DB, you can also include this in the app file:

if __name__ == "__main__":
    db.create_all()
    app.debug == True
    app.run()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading