Flask Tutorial
About Lesson

In this Flask Tutorial we are going to learn about Working with Flask SQLAlchemy, we are going to learn how you can use SQLAlchemy and how you can connect that with MySQL database.



What is SQLAlchemy ?

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL. It provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.



What is Flask-SQLAlchemy ?

Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application. It aims to simplify using SQLAlchemy with Flask by providing useful defaults and extra helpers that make it easier to accomplish common tasks. See the SQLAlchemy documentation to learn how to work with the ORM in depth.




You can simply install Flask-SQLAlchemy using pip command.



Because we are using MySQL database, you need to install mysqlclient.




So now after installation you need to create database, we are using Mysql database. for this you need to download and install Wamp Server. create a database in the Wamp Server, iam going to name it myflask for the database name, but you can give the name according to your choice. right now we don’t have any table in our database, we will create that using Flask SQLAlchemy. 



You can add this code for SQLAlchemy configuration in your app.py file, you can see that we are using MySQL database, you need to give your database type, username, password and database name, in our case it is myflask




Using this code you can create your model or we can say our database table.




This is the complete code for app.py file.




Now open your Python terminal and write these commands for creating the table in your database.




Check your Wamp Server and now we have our user_info table in our database.

Flask Tutorial - Working with Flask SQLAlchemy
Flask Tutorial – Working with Flask SQLAlchemy



Also you can add data to your table using this command.



Rest of the files for the tutorial.