Question
How do I carry out below query in SQLAlchemty:
- testdb=# SELECT * FROM students;
- id | name | lastname
- ----+-------+----------
- 1 | Ravi | Kapoor
- 2 | Rajiv | Khanna
- 3 | Komal | Bhandari
- 4 | Abdul | Sattar
- 5 | Priya | Rajhans
- (5 rows)
- testdb=# SELECT * FROM students WHERE name LIKE 'R%';
- id | name | lastname
- ----+-------+----------
- 1 | Ravi | Kapoor
- 2 | Rajiv | Khanna
- (2 rows)
Consider we have base.py:
- from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String
- from sqlalchemy import select, join, and_, or_
- db_string = "postgresql://postgres:password@localhost/testdb"
- engine = create_engine(db_string, echo = True)
- meta = MetaData(bind=engine)
- meta.reflect()
- conn = engine.connect()
- students = meta.tables['students']
- addresses = meta.tables['addresses']
- employee = meta.tables['employee']
Supplement
* SQLAlchemy doc - Query API
* SQLAlchemy ColumnOperators like
沒有留言:
張貼留言