Beispiel #1
0
#!/usr/bin/python3
'''add the state object Louisianna'''

if __name__ == '__main__':
    from sys import argv
    from model_state import Base, State
    from sqlalchemy import (create_engine)
    from sqlalchemy.orm import sessionmaker as sm

    engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format(
        argv[1], argv[2], argv[3]),
                           pool_pre_ping=True)
    Base.metadata.create_all(engine)

    # define parameters for session
    Session = sm(bind=engine)

    # create session
    session = Session()

    # create new state object
    new_state = State(name='Louisiana')

    # add our new state
    session.add(new_state)

    # commit changes to session
    session.commit()

    # print our result, formatted correctly
    print(new_state.id)
Beispiel #2
0
def _base__repr__(self):
    import pprint
    obj = vars(self)
    output = {}
    for i in self.__mapper__.columns.keys():
        try:
            output[i] = obj[i]
        except KeyError:
            pass
    return pprint.saferepr((output, self.__mapper__.relationships.keys()))


Base.__repr__ = _base__repr__

from sqlalchemy.orm import scoped_session as ss, sessionmaker as sm
scoped_session = ss(sm())


def init(data_path):

    global scoped_session
    engine = None
    if not os.path.isfile(data_path):
        _logger.info("Database doesn't exist, initializing")
        # Import these so ORM builds tables
        import xbx.config
        import xbx.build
        import xbx.run
        import xbx.run_op
        engine = create_engine('sqlite:///' + data_path)
        Base.metadata.create_all(engine)