マインドマップギャラリー SQLアルケミー
これは SQLAlchemy に関するマインド マップです。主な内容は、例外処理、パフォーマンスの最適化、関係、データの削除、データの更新、データのクエリ、データの挿入、データベースへのマッピング、モデルの宣言、データベースへの接続、インストール、導入です。
2024-01-30 11:35:57 に編集されました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!
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!
SQLアルケミー
導入
SQLAlchemy は Python SQL ツールキットおよび ORM フレームワークです
包括的なデータベース操作とORM機能を提供します
MySQL、PostgreSQL、SQLite などの複数のデータベースをサポートします。
インストール
pip を使用して SQLAlchemy をインストールする
インストールコマンド: pip install SQLAlchemy
データベースに接続する
create_engine 関数を使用してデータベース接続を作成する
例: Engine = create_engine('mysql://user:password@localhost/dbname')
モデルの宣言
Declarative_base 関数を使用してモデルの基本クラスを作成する
クラスを使用してモデルの基本クラスを継承し、プロパティを定義します
例: sqlalchemy インポートから列、整数、文字列
クラス ユーザー(ベース):
__テーブル名__ = 'ユーザー'
id = 列(整数、primary_key=True)
名前 = 列(文字列)
データベースへのマッピング
Base.metadata.create_all() を使用してモデルをデータベースにマッピングします
例: Base.metadata.create_all(engine)
データの挿入
session.add() を使用して新しいオブジェクトをセッションに追加します
***mit() を使用してセッションをコミットします
例: sqlalchemy.orm から sessionmaker をインポート
セッション = セッションメーカー(バインド = エンジン)
セッション = セッション()
new_user = ユーザー(name='ジョン')
session.add(新しいユーザー)
***ミット()
クエリデータ
session.query() を使用してクエリ オブジェクトを構築する
クエリ オブジェクトの filter メソッドを使用してクエリを実行する
例: users = session.query(User).filter_by(name='John').all()
データを更新する
session.query() を使用してクエリ オブジェクトを構築する
クエリオブジェクトのupdateメソッドを使用して更新します。
例: user = session.query(User).filter_by(name='John').first()
user.name = 'ジェーン'
***ミット()
データを削除する
session.query() を使用してクエリ オブジェクトを構築する
クエリオブジェクトの delete メソッドを使用して削除します
例: user = session.query(User).filter_by(name='Jane').first()
セッション.削除(ユーザー)
***ミット()
関係
一対一の関係
ForeignKey を使用して 1 対 1 の関係を定義する
例: sqlalchemyからForeignKeyをインポート
クラス ユーザー(ベース):
__テーブル名__ = 'ユーザー'
id = 列(整数、primary_key=True)
address_id = Column(整数,ForeignKey('addresses.id'))
1対多の関係
1 対多の関係を定義するには、ForeignKey を使用します。
例: sqlalchemyからForeignKeyをインポート
クラス ユーザー(ベース):
__テーブル名__ = 'ユーザー'
id = 列(整数、primary_key=True)
address_id = Column(整数,ForeignKey('addresses.id'))
多対多の関係
関連テーブルを使用して多対多の関係を定義する
例: sqlalchemy からのインポート テーブル、列、整数、ForeignKey
users_addresses = Table('users_addresses',
Base.metadata、
列('user_id', 整数, 外部キー('users.id')),
列('アドレスID', 整数, 外部キー('アドレス.ID'))
)
パフォーマンスの最適化
サブクエリの代わりに結合クエリを使用する
単一挿入の代わりにバッチ挿入を使用する
インデックスを使用してクエリ速度を向上させる
例外処理
Try/Except を使用してデータベース操作の例外を処理する
例: 次を試してください:
session.add(新しいユーザー)
***ミット()
e としての例外を除く:
session.rollback()
印刷(e)