I am new to flask. I am developing a web application using flask and postgres. I have already designed database. I know sqlalchemy is orm. Do I still need to use sqlalchemy in flask if my database was already designed without sqlalchemy. I have to use that database for fetching and updating values. While going further will sqlalchemy is useful to me? or can I simply use db connector and proceed?
Flask is a web framework and does not require SQLAlchemy. If you do choose to use Flask and SQLAlchemy, there is the Flask-SQLAlchemy extension to make them work together better.
Do you need to use SQLAlchemy, or any ORM, to work with a database? No. Should you? Yes. ORMs bring certain features and conveniences that raw SQL does not provide. And you can always write raw SQL.
Here are some important benefits of SQLAlchemy.
While there is a SQL standard, all SQL databases are different. An ORM smooths over many of these differences so your SQLAlchemy code will be easier to migrate to different databases.
Your schema is written, but what if you need to change it? That's where database migrations come in. They allow you to change your schema (adding and removing columns, changing types, adding tables) in a manner that is reproducible and reversible. This makes it easier and safer to change your database, and the data in it, along with your code. As well as to reverse out of changes that turned out to be bad ideas.
ORM stands for "Object-Relational Mapper". It lets you treat a database as a bunch of objects rather than just some data. Python is an object-oriented language, and your data will be much easier to work with as objects with methods. Each table is a class, each row is an object.
The "mapping" part doesn't just apply to rows, it also applies to SQL data types. For example, timestamp columns will be loaded as Python as datetime objects. This makes it more convenient to work with the data in Python.
SQLAlchemy allows you to validate your data before you insert it into the database ensuring all your data is as you expect. Knowing everything in the database is valid makes the system more robust and cuts down on your error handling.
Instead of having to write all your SQL by hand, SQLAlchemy has a query builder. You can build and modify queries as objects. This makes writing queries simpler. And it lets you build them up in pieces. For example, you can start with a query.
all_users = select([
users.c.name,
users.c.email
])
And then add a where clause.
a_users = all_users.where(users.c.name.ilike("a%"))
And then a limit clause.
some_a_users = a_users.limit(10)
Using sqlalchemy will help you fetching/inserting data very easy, no matter you designed your db manually, you just need to define your design and then instead of writing multiple lines, you write a line and everything done. It also help you handle errors and a lot more. Strongly suggest you to use it.