In order to interact with the database, we want to attain its deal with. A session item is the manage to database. Session elegance is described using sessionmaker() – a configurable consultation factory method that is sure to the engine item created earlier.
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind = engine)
The consultation item is then set up the usage of its default constructor as follows −
session = Session()
Some of the often required methods of session elegance are listed beneath −
Sr.No. | Method & Description |
---|---|
1 |
begin() begins a transaction on this session |
2 |
add() places an object in the session. Its state is persisted in the database on next flush operation |
3 |
add_all() adds a collection of objects to the session |
4 |
commit() flushes all items and any transaction in progress |
5 |
delete() marks a transaction as deleted |
6 |
execute() executes a SQL expression |
7 |
expire() marks attributes of an instance as out of date |
8 |
flush() flushes all object changes to the database |
9 |
invalidate() closes the session using connection invalidation |
10 |
rollback() rolls back the current transaction in progress |
11 |
close() Closes current session by clearing all items and ending any transaction in progress |