MindMap Gallery SQLAlchemy
This is a mind map about SQLAlchemy. The main contents include: exception handling, performance optimization, relationships, deleting data, updating data, querying data, inserting data, mapping to database, declaring model, connecting to database, installation, introduction.
Edited at 2024-01-30 11:35:57Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
SQLAlchemy
Introduction
SQLAlchemy is a Python SQL toolkit and ORM framework
Provides comprehensive database operations and ORM functions
Supports multiple databases, such as MySQL, PostgreSQL, SQLite, etc.
Install
Install SQLAlchemy using pip
Installation command: pip install SQLAlchemy
Connect to the database
Create a database connection using the create_engine function
Example: engine = create_engine('mysql://user:password@localhost/dbname')
Declare model
Use the declarative_base function to create a model base class
Use a class to inherit the model base class and define properties
Example: from sqlalchemy import Column, Integer, String
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
Map to database
Use Base.metadata.create_all() to map the model to the database
Example: Base.metadata.create_all(engine)
Insert data
Use session.add() to add new objects to the session
Use ***mit() to commit the session
Example: from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
new_user = User(name='John')
session.add(new_user)
***mit()
Query data
Use session.query() to construct a query object
Query using the filter method on the query object
Example: users = session.query(User).filter_by(name='John').all()
update data
Use session.query() to construct a query object
Update using the update method on the query object
Example: user = session.query(User).filter_by(name='John').first()
user.name = 'Jane'
***mit()
delete data
Use session.query() to construct a query object
Delete using delete method on query object
Example: user = session.query(User).filter_by(name='Jane').first()
session.delete(user)
***mit()
relation
one-to-one relationship
Define one-to-one relationship using ForeignKey
Example: from sqlalchemy import ForeignKey
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
address_id = Column(Integer, ForeignKey('addresses.id'))
one-to-many relationship
Use ForeignKey to define a one-to-many relationship
Example: from sqlalchemy import ForeignKey
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
address_id = Column(Integer, ForeignKey('addresses.id'))
many-to-many relationship
Define many-to-many relationships using association tables
Example: from sqlalchemy import Table, Column, Integer, ForeignKey
users_addresses = Table('users_addresses',
Base.metadata,
Column('user_id', Integer, ForeignKey('users.id')),
Column('address_id', Integer, ForeignKey('addresses.id'))
)
Performance optimization
Use join query instead of subquery
Use batch insert instead of single insert
Use indexes to improve query speed
Exception handling
Use try/except to handle database operation exceptions
Example: try:
session.add(new_user)
***mit()
except Exception as e:
session.rollback()
print(e)