Ejemplo n.º 1
0
def create_session(engine):
    # Creating a session
    from sqlalchemy.orm import sessionmaker
    Session = sessionmaker(bind=engine)
    session = Session()
    return session

if __name__ == '__main__':
    from sqlalchemy import create_engine
    engine = create_engine('sqlite:///:memory:', echo=True)
    create_model(engine)

    session = create_session(engine)

    ed_user = create_user()

    print ed_user.id  # None

    print "** Adding user to session."
    session.add(ed_user)

    our_user = session.query(User).filter_by(name='ed').first()
    print "** Are users equal?", ed_user is our_user

    print "** Comitting session."
    session.commit()

    print "** Printing ed_user id:"
    print ed_user.id  # 1
Ejemplo n.º 2
0
from example1 import User
from example2 import create_model
from example3 import create_user
from example4 import create_session


if __name__ == '__main__':
    from sqlalchemy import create_engine
    engine = create_engine('sqlite:///:memory:', echo=True)
    create_model(engine)

    session = create_session(engine)
    one_user = create_user()
    session.add(one_user)
    another_user = create_user()
    session.add(another_user)
    session.commit()

    for some_user in session.query(User):
        print "** Iterating over users"
        print some_user


    print "** Counting all users"
    print session.query(User).count()

    print "** Counting all users another way"
    print len(session.query(User).all())