0

How to simulate a SELECT in SQLAlchemy? I would like to create a function which takes a couple of parameters and returns a row which contains those values but I can't do SELECT.

The only way I found is below but I can't find metadata in SQLAlchemy module.

EDIT: I figured out that BoundMetaData is deprecated so MetaData is appropriate, but it says that Select has no len

# -*- coding: utf-8 -*- import sqlalchemy from sqlalchemy import Column, Table from sqlalchemy import UniqueConstraint from sqlalchemy import create_engine from sqlalchemy.exc import IntegrityError from sqlalchemy.ext.declarative import declarative_base engine = create_engine('sqlite:///db.db', echo=False) Base = declarative_base() s = sqlalchemy.orm.Session(engine) class Flight(Base): __tablename__ = 'flights' id = Column(sqlalchemy.Integer, primary_key=True) destination_from = Column(sqlalchemy.String) destination_to = Column(sqlalchemy.String) creation_date = Column(sqlalchemy.Date) start_date = Column(sqlalchemy.Date) return_date = Column(sqlalchemy.Date) price = Column(sqlalchemy.Float) filename = Column(sqlalchemy.String) bought_days_before = Column(sqlalchemy.Integer) __table_args__ = ( UniqueConstraint('creation_date', 'destination_from', 'destination_to', 'start_date', 'return_date', 'price'), ) Base.metadata.create_all(engine) def insert_into_flights(**kwargs): s.add(Flight(**kwargs)) try: s.commit() except IntegrityError as e: s.rollback() def get_prices(date_from, days, bought_days_before, destination, min=True, avg=False): flights = Table('flights', metadata ,autoload=True ) print len(flights.select()) 
2
  • What do you mean by "simulate"? For what purpose are you trying to "simulate"? Commented Apr 4, 2016 at 20:38
  • Just wrong word. I wanted to do SELECT as Jesse Baker answered. Commented Apr 4, 2016 at 20:40

1 Answer 1

2
s.query(Flight).filter(Flight.id==34).all() 

This is an example selecting the Flight with id 34. See SQLAlchemy docs

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.