I'm currently writing an program using Flask python. But I can not connect my database with my program. I'm use Postgresql for database.
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine(os.getenv("DATABASE_URI"))
db = scoped_session(sessionmaker(bind=engine))
This is the code what I'm using for it, But it didn't work. So please help me to connect my database
This is the error message that what I have got,
Error message
user16298377 :
Configure database string\nfrom sqlalchemy import create_engine\nengine = create_engine('postgresql+psycopg2://user:password@hostname/database_name')\n\nConnect db then:\nimport psycopg2\nconn_string = "host='localhost' dbname='my_database' user='postgres' password='secret'"\nconn = psycopg2.connect(conn_string)\n",
2021-07-16T13:31:04
Akshay Jain :
db.py File which contains db connection(postgres url)\n\nfrom flask import Flask\nfrom flask_sqlalchemy import SQLAlchemy\n\napp = Flask(__name__)\napp.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://postgres:postgres@localhost/DB_NAME'\n\ndb = SQLAlchemy(app)\n\n\napp.py This is your main file database tables will create when your server hits the first request.\n\nfrom db import *\
[email protected]_first_request\ndef before():\n db.create_all()\n\nenter code here......\n\n\n\ntable_structure.py This file contains your table structure\n\nfrom db import db\nclass UserProjects(db.Model):\n id = db.Column(db.Integer, primary_key=True)\n name = db.Column(db.String(140), nullable=False)\n\n",
2020-05-31T12:42:14