SQL stands for structured query language and is used to communicate with a database. It is used to update and retrieve data from a database.
A database is used to store.. well.. data. Databases have three elements which are Tables, Records and Fields. A “Table” is a collection of a particular type of data. This could be “Users” or “Customers”. Records are also called rows because they basically represent one row in the table. A field is a part of the record and is a single piece of data.
Difference between Non-Relational and Relational database
Relational databases store data in tables are very structured and can be connected together while Non-Relational databases are like documents and store data as categories.
Working with Non-Relational databases is a lot more flexible and inspires speed and agility. While working with Relational databases is simpler because with just some basic SQL queries you can easily access data. All data should respond to clear regulations that are the same for all DBMS while working with Relational Databases. This unfortunately means the initialization takes a lot longer. But this also means they are a lot more structured.
Examples of Relational Database: PostgreSQL, MySQL
Examples of Non-Relational Database: MongoDB, Cassandra
What is SQL Alchemy?
Lets look at a simple SQL Query:-
SELECT * FROM Customers WHERE email = '[email protected]';
If you’ve worked with SQL before this is a very simple query and quite easy to understand. However using ORM you can convert this into a object oriented approach of your programing language which in this case is python.
This is how the query would look like in python using SQL Alchemy:-
session.query.filter(Customers.email == '[email protected]')
According to sqlalchemy.org:- A successful application may be constructed using the Object Relational Mapper exclusively. In advanced situations, an application constructed with the ORM may make occasional usage of the Expression Language directly in certain areas where specific database interactions are required.
Its always a good idea to learn a bit about some SQL commands get rid of the initial confusion and familiarize yourself with the direct process. W3schools has a great series which goes in depth about SQL here.
- SQL Alchemy chapter from the book, Architecture of Open Source Applications: http://aosabook.org/en/sqlalchemy.html
- Cheat-sheet: https://github.com/crazyguitar/pysheeet/blob/master/docs/notes/python-sqlalchemy.rst
What is Flask-SQLAlchemy?
If you dig into Flask SQLAlchemy without much knowledge of working with ORM like it did you might notice even if it feels easy in the beginning soon enough you start having issues which take ages to resolve. This is why I’d recommend you to start from SQL move to SQL Alchemy and then Flask-SQLAlchemy will be a breeze.
Other than few initialization requirements there’s not much of a difference here which is a good thing because there’s a lot more material available for SQLAlchemy which should make it very easy to solve most of your issues.
This section from the official site for Flask-SQLAlchemy makes things clear about everything you really need to know while using this extension.
Road to Enlightenment
The only things you need to know compared to plain SQLAlchemy are:
SQLAlchemygives you access to the following things:
- all the functions and classes from
- a preconfigured scoped session called
SQLAlchemy.drop_all()methods to create and drop tables according to the models.
Modelbaseclass that is a configured declarative base.
- all the functions and classes from
Modeldeclarative base class behaves like a regular Python class but has a
queryattribute attached that can be used to query the model. (
- You have to commit the session, but you don’t have to remove it at the end of the request, Flask-SQLAlchemy does that for you.