Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.
Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.
For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Data Science and Machine Learning - 16 wks
Full-Stack Software Developer - 16w
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
SQL Alchemy is an Object-Relational Mapper/Mapping-tool, or ORM: a library that developers use to create databases and manipulate their data without the need of knowing/using SQL.
There are other alternatives in Python like Peewee, and other languages have their own ORM's like PHP Eloquent or Java Hibernate.
ORM's have gained popularity because dealing with SQL language directly requires a lot of effort in many cases. The goal of any ORM is to simplify the maintenance of your data. This is done by creating objects to deal with database interactions.
With ORM's you won't have to type SQL again (95% of the time) and you will be able to work with objects.
To insert a user with SQL, you have to type:
With an ORM your code keeps being familiar like this:
Just use the
db.session.commit() function and everything you have done with your code will be translated into SQL language code.
To use SQL Alchemy, we need to install the Python
flask library. Once we have done that, we will establish a connection to the database and define the
db object, which is the most important thing to start working with.
The first step will be defining our model:
To insert a record in the database, it is first necessary to have the instance to be added. Then, add it to the database session and complete the action with a commit. The following code visualizes this functionality (replace
<email_value> with the actual values you want to add):
There are 3 ways to retrieve data from a database:
Person.query.filter_by(arg1=value, arg2=value, ...)
To delete a record from the database, it is necessary to previously select the instance to be deleted (through its primary key, the id) and delete it using
db.session.delete(person), according to the following example:
To modify a record, you must first select it from the database, then you can work with it by changing its properties and commit it again, as shown in the following example:
A transaction is a sequence of operations (such as INSERT, UPDATE, SELECT) performed on your database. For a transaction to be complete, all operations must be successful. If one operation fails, the whole transaction fails.
All transactions must ensure 4 main properties (known as ACID properties): Atomicity, Consistency, Isolation and Durability.
A transaction ends with
COMMIT command is used to permanently save the changes made in a transaction within the database.
When you use INSERT, UPDATE or DELETE, the changes made with these commands are not permanent, the changes made can be undone or, in other words, we can go back.
However, when you use the COMMIT command, the changes in your database will be permanent.
ROLLBACK command restores your database to your last COMMIT. You can also use it with the SAVEPOINT command to jump to a point you saved during an ongoing transaction.
Similarly, if you use UPDATE to make changes to your database, you can undo them using the ROLLBACK command, but only if you have not yet used the COMMIT command.
SAVEPOINT command is used to temporarily save a transaction so that you can return to a certain point using the ROLLBACK command if you need to. You can use it like this:
This command can be called many times, and with each call, a checkpoint called
checkpoint is established and has a unique identifier associated with it.
Let's take, for example, the case that we want to prepare a pizza, and we prepare a database in which to enter the ingredients it contains. The base of this pizza that we want to prepare has three ingredients: mozzarella, tomato and olives. Our table is going to be called 'Pizza' and, after inserting the ingredients, it would look like this:
In addition, we have a list of extra ingredients we can add: we choose meat first, but then change our mind and want to replace it with mushrooms. We will also add pepperoni and bacon. Let's see how this transaction would be done:
Now, our 'Pizza' has the following ingredients:
However, before putting it in the oven, we decided we didn't want bacon, so we used the rollback:
Finally, our 'Pizza' looks like this:
... I'm a bit hungry after reading this lesson!! Aren't you??