示例#1
0
def prepare_data(engine):
    orm.start_mappers()
    orm.metadata.drop_all(engine)
    orm.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()

    # add users
    session.add(
        model.User(username='******', password='******',
                   start_date='2021-05-14'))
    session.add(
        model.User(username='******',
                   password='******',
                   start_date='2021-05-15'))
    session.add(
        model.User(username='******',
                   password='******',
                   start_date='2021-05-30'))
    session.commit()

    # add words
    session.add(
        model.NewWord(username='******', word='starbucks', date='2021-05-15'))
    session.add(
        model.NewWord(username='******', word='luckin', date='2021-05-15'))
    session.add(
        model.NewWord(username='******', word='secondcup', date='2021-05-15'))
    session.add(
        model.NewWord(username='******', word='costa', date='2021-05-15'))
    session.add(
        model.NewWord(username='******', word='timhortons', date='2021-05-15'))
    session.add(
        model.NewWord(username='******', word='frappuccino', date='2021-05-15'))
    session.commit()

    # add articles
    article = model.Article(
        article_id=1,
        text=
        'THE ORIGIN OF SPECIES BY MEANS OF NATURAL SELECTION, OR THE PRESERVATION OF FAVOURED RACES IN THE STRUGGLE FOR LIFE',
        source='CHARLES DARWIN, M.A.',
        date='1859-01-01',
        level=5,
        question='Are humans descended from monkeys?')
    session.add(article)

    article = model.Article(article_id=2,
                            text='THE ELEMENTS OF STYLE',
                            source='WILLIAM STRUNK JR. & E.B. WHITE',
                            date='1999-01-01',
                            level=4,
                            question='Who may benefit from this book?')
    session.add(article)

    session.commit()

    session.close()

    clear_mappers()
def session_factory():
    engine = create_engine('sqlite://')
    orm.metadata.create_all(engine)

    orm.start_mappers()

    yield sessionmaker(bind=engine)

    clear_mappers()
示例#3
0
# Software Architecture and Design Patterns -- Lab 2 starter code
# Copyright (C) 2021 Hui Lan

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

import model
import orm

orm.start_mappers()
engine = create_engine(
    r'sqlite:///D:\newDesktop\大三下courses\SADP\lab2\test\EnglishPalDatabase.db') # modify the path
orm.metadata.drop_all(engine)
orm.metadata.create_all(engine)
get_session = sessionmaker(bind=engine)

# add two users

session = get_session()

try:
    session.add(model.User(username='******', password='******', start_date='2021-05-14'))
    session.add(model.User(username='******', password='******', start_date='2021-05-15'))
    session.commit()
except:
    print('Duplicate insertions.')

print(session.query(model.User).count())

for u in session.query(model.User).all():
    print(u.username)
示例#4
0
def session(in_memory_db):
    start_mappers()
    yield sessionmaker(bind=in_memory_db)()
    clear_mappers()
示例#5
0
def session(the_db):
    start_mappers()
    # Session = sessionmaker(bind=the_db)
    # the_session = Session()
    yield sessionmaker(bind=the_db)()
    clear_mappers()
示例#6
0
def get_session(engine):
    orm.start_mappers()
    yield sessionmaker(bind=engine)
    clear_mappers()
示例#7
0
def postgres_session(postgres_db):
    start_mappers()
    yield sessionmaker(bind=postgres_db)()
    clear_mappers()
示例#8
0
def postgres_session(sqllite_db):
    start_mappers()
    yield sessionmaker(bind=sqllite_db)()
    clear_mappers()
 def get_session(cls):
     if cls.engine is None:
         cls.engine = create_engine(DB_URL)
         orm.metadata.create_all(cls.engine)
         orm.start_mappers()
     return sessionmaker(bind=cls.engine)()