Explain the database connection in python flask?
Answer / Durga Datt Upadhyay
In Flask, you can use extensions to handle database connections. One such extension is Flask-SQLAlchemy. First, you'll need to install SQLAlchemy and then initialize it in your application. Then, you can define a database URI, create a database object, and define your models.nnHere's an example:nn```pythonnfrom flask import Flasknfrom flask_sqlalchemy import SQLAlchemynnapp = Flask(__name__)napp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' ndb = SQLAlchemy(app)nn# Define a model classnclass User(db.Model):n id = db.Column(db.Integer, primary_key=True)n username = db.Column(db.String(80), unique=True, nullable=False)npassword = db.Column(db.String(120))nn# Create the database tablesndb.create_all()```
| Is This Answer Correct ? | 0 Yes | 0 No |
What are python slots?
What is range() in python? Give an example to explain it.
Is python easy to learn?
Is python a single inheritance?
What is GIL?
What is python basic syntax?
Are tuples mutable?
Does return print in python?
Explain about membership operator in python?
What is python constructor?
What is pytest in python?
How dir() function is used in python?