from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from mymodels import User engine = create_engine('postgresql://user:password@localhost/mydatabase') Session = sessionmaker(bind=engine) session = Session() new_user = User(name='John Doe', email='[email protected]') session.add(new_user) session.commit()
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from mymodels import User engine = create_engine('postgresql://user:password@localhost/mydatabase') Session = sessionmaker(bind=engine) session = Session() users = session.query(User).filter_by(name='John Doe').all() for user in users: print(user.name, user.email)In this example, we create a new session and use it to retrieve all records from the `User` table where the `name` field is equal to 'John Doe'. We then loop over the records and print out the `name` and `email` fields. Package library: SQLAlchemy ORM is the package library used in these examples.