Hortitude
Hortitude

Reputation: 14068

accessing mysql from within flask

I noticed that most examples for accessing mysql from flask suggest using a plugin that calls init_app(app).

I was just wondering why that is as opposed to just using a mysql connector somewhere in your code as you need it?

Is it that flask does better resource management with request life cycles?

Upvotes: 1

Views: 119

Answers (1)

Haifeng Zhang
Haifeng Zhang

Reputation: 31925

Using packages like flask-mysql or Flask-SQLAlchemy, they provided useful defaults and extra helpers that make it easier to accomplish common CRUD tasks.

All of such package are good at handling relationships between objects. You only need to create the objects and then the objects contain all the functions and helpers you needed to deal with the database, you don't have to implement such code by yourself and you don't need to worry about the performance of the queries.

I had worked on a Django project(I believe the theory in Flask is similar) and its ORM is really amazing, all i need to do is writing Models and encapsulate business logic. All CRUD commands are handled by the built-in ORM, as a developer we don't worry about the SQL statements.

Another benefit is that it makes database migration much easier. You can switch it from MySQL to PostgresSQL with minimal code modifications which will speed up development.

Upvotes: 3

Related Questions