Explain the database connection in python flask?



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

Post New Answer

More Python Interview Questions

What are python slots?

1 Answers  


What is range() in python? Give an example to explain it.

1 Answers  


Is python easy to learn?

1 Answers  


Is python a single inheritance?

1 Answers  


What is GIL?

2 Answers  


What is python basic syntax?

1 Answers  


Are tuples mutable?

1 Answers  


Does return print in python?

1 Answers  


Explain about membership operator in python?

1 Answers  


What is python constructor?

1 Answers  


What is pytest in python?

1 Answers  


How dir() function is used in python?

1 Answers  


Categories