SQLAlchemy is a popular Python library that provides an Object-Relational Mapping (ORM) tool for working with relational databases. It allows you to interact with databases using Python objects and provides a high-level, abstracted interface to perform CRUD (Create, Read, Update, Delete) operations without writing raw SQL queries.
Overview of SQLAlchemy:
Most common methods in SQLAlchemy (ORM):
session.add()
: Adds an object to the session’s unit of work, meaning it will be persisted to the database upon committing the session.session.query()
: Creates a new query object that allows you to fetch data from the database using Pythonic syntax.session.commit()
: Commits the current transaction to the database, making the changes permanent.session.rollback()
: Rolls back the current transaction, undoing any changes made since the last commit.session.delete()
: Marks an object for deletion in the database. The object will be deleted upon committing the session.session.merge()
: Merges changes from a detached object into the session’s unit of work, updating the corresponding object in the database.session.expunge()
: Removes an object from the session, making it “detached” from the session and no longer tracked for changes.session.query.filter()
: Adds filtering criteria to the query to retrieve specific data from the database.session.query.join()
: Performs a join operation between two or more tables in the database.These are just a few of the common methods provided by SQLAlchemy. The library is quite extensive and offers many more features for working with databases in Python.