Tutorial by Examples

The engine is used to connect to different databases using a connection URL: from sqlalchemy import create_engine engine = create_engine('postgresql://user:pass@localhost/test') Note, however, that the engine does not actually establish a connection until it is first used. The engine automat...
You can open a connection (i.e. request one from the pool) using a context manager: with engine.connect() as conn: result = conn.execute('SELECT price FROM products') for row in result: print('Price:', row['price']) Or without, but it must be closed manually: conn = engine.co...
If you only want to execute a single statement, you can use the engine directly and it will open and close the connection for you: result = engine.execute('SELECT price FROM products') for row in result: print('Price:', row['price'])
You can use engine.begin to open a connection and begin a transaction that will be rolled back if an exception is raised, or committed otherwise. This is an implicit way of using a transaction, since you don't have the option of rolling back manually. with engine.begin() as conn: conn.execute(...

Page 1 of 1