def rel_state_city(): engine = create_engine("mysql+mysqldb://{}:{}@localhost/{}" .format(argv[1], argv[2], argv[3]), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() new_state = State(name='California') new_state.cities = [City(name='San Francisco')] session.add(new_state) session.commit() session.close()
def create_cal(): """creates the state california""" engine = create_engine('mysql+mysqldb://{}:{}@localhost:3306/{}'.format( sys.argv[1], sys.argv[2], sys.argv[3])) Session = sessionmaker(bind=engine) session = Session() state = State(name="California") state.cities = [City(name="San Francisco")] session.add(state) session.commit() session.close()
def relationship_states_cities(): """ Creates the State “California” with the City “San Francisco” in the database. Takes 3 arguments: mysql username, mysql password and database name. """ engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( sys.argv[1], sys.argv[2], sys.argv[3]), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() new_state = State(name="California") new_state.cities = [City(name="San Francisco")] session.add(new_state) session.commit() session.close()
#!/usr/bin/python3 """ post """ from relationship_state import Base, State from sys import argv import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from relationship_city import City if __name__ == "__main__": engine = create_engine('mysql+mysqldb://{}:{}@localhost:3306/{}'.format( argv[1], argv[2], argv[3])) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() st = State(name="California") st.cities = [City(name="San Francisco")] session.add(st) session.commit() session.close()
#!/usr/bin/python3 ''' Task 100 ''' from relationship_state import Base, State from sqlalchemy import create_engine, select, Table, MetaData import sys from sqlalchemy.orm import sessionmaker from relationship_city import City if __name__ == '__main__': MY_HOST = "localhost" MY_USER = sys.argv[1] MY_PASS = sys.argv[2] MY_DB = sys.argv[3] m = "mysql://{}:{}@{}:3306/{}".format(MY_USER, MY_PASS, MY_HOST, MY_DB) engine = create_engine(m, encoding='latin1') conn = engine.connect() Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() cali = State(name='California') cali.cities = [City(name='San Francisco')] session.add(cali) session.commit() session.close()
#!/usr/bin/python3 """ lists all states from states table """ from sqlalchemy.orm import sessionmaker from relationship_state import State, Base from relationship_city import City from sqlalchemy import create_engine import sys if __name__ == "__main__": engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( sys.argv[1], sys.argv[2], sys.argv[3]), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() obj = State(name="California") obj.cities = [City(name="San Francisco", state_id=str(obj.id))] session.add(obj) session.commit()
#!/usr/bin/python3 """This module ceates the state California with the City San Francisco in the database passed as argument""" if __name__ == "__main__": from relationship_city import City from relationship_state import Base, State from sys import argv from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( argv[1], argv[2], argv[3], pool_pre_ping=True)) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() newState = State(name="California") newCity = City(name="San Francisco") newState.cities = [newCity] session.add(newState) session.commit() session.close()
#!/usr/bin/python3 """ lists all state objects in database """ import sys from relationship_state import Base, State from relationship_city import City from sqlalchemy import (create_engine) from sqlalchemy.orm import sessionmaker if __name__ == "__main__": engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}\ '.format(sys.argv[1], sys.argv[2], sys.argv[3]), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() newstate = State(name="California") newstate.cities = [City(name="San Francisco")] session.add(newstate) session.commit() session.close()
#!/usr/bin/python3 """Improve the files model_city.py and model_state.py, and save them as relationship_city.py and relationship_state.py """ import sys from relationship_state import Base, State from relationship_city import City from sqlalchemy import create_engine from sqlalchemy.orm import Session if __name__ == "__main__": username = sys.argv[1] password = sys.argv[2] database = sys.argv[3] engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( username, password, database), pool_pre_ping=True) Base.metadata.create_all(engine) session = Session(engine) new_state = State(name='California') new_state.cities = [City(name='San Francisco')] session.add(new_state) session.commit() session.close()
#!/usr/bin/python3 """ Add a new state and a city relationed with it """ import sys from relationship_state import State from relationship_city import City, Base from sqlalchemy.orm import sessionmaker from sqlalchemy import (create_engine) if __name__ == "__main__": engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( sys.argv[1], sys.argv[2], sys.argv[3]), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() California = State(name="California") California.cities = [City(name="San Francisco")] session.add(California) session.commit() session.close()
""" from sys import argv from relationship_state import State, Base from relationship_city import City from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker if __name__ == "__main__": User = argv[1] Passwd = argv[2] Db = argv[3] Host = "localhost" Engine = create_engine("mysql+mysqldb://{}:{}@{}/{}".format( User, Passwd, Host, Db)) Base.metadata.create_all(Engine) Session = sessionmaker(bind=Engine) session = Session() newSt = State(name='California') newCt = City(name='San Francisco') newSt.cities = [newCt] session.add(newSt, newCt) session.commit() session.close()
def createCascadeRecord(conn): '''Creates a new state with an associated city''' state = State(name='California') state.cities = [City(name='San Francisco')] conn.add(state) conn.commit()
#!/usr/bin/python3 """ add new state "California" and new city "San francisco" to the database hbtn_0e_6_usa""" from sqlalchemy import create_engine from sqlalchemy.orm.session import Session, sessionmaker from relationship_state import Base, State from relationship_city import Base, City import sys if __name__ == '__main__': engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( sys.argv[1], sys.argv[2], sys.argv[3]), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() new_state = State(name="California") new_state.cities = City(name="San Francisco") query_row = session.add(new_state) session.commit() session.close()
#!/usr/bin/python3 """ Lists all cities from the database """ if __name__ == "__main__": from sys import argv from relationship_state import Base, State from relationship_city import City from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker engine = create_engine('mysql+mysqldb://{}:{}@localhost:3306/{}'.format( argv[1], argv[2], argv[3])) Base.metadata.create_all(engine) session = sessionmaker(bind=engine)() cali = State(name='California') sf = City(name='San Francisco') cali.cities = [sf] session.add(cali) session.commit()
#!/usr/bin/python3 """ 11-model_state_insert module """ if __name__ == "__main__": import sys from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from relationship_state import Base, State from relationship_city import City inp = sys.argv if len(inp) < 4: exit(1) conn_str = "mysql+mysqldb://{}:{}@localhost:3306/{}" engine = create_engine(conn_str.format(inp[1], inp[2], inp[3])) Session = sessionmaker(engine) Base.metadata.create_all(engine) session = Session() california = State('California') san_fran = City('San Francisco') california.cities = [san_fran] session.add(california) session.commit() session.close()
#!/usr/bin/python3 """ Add the State object 'California' with the city 'San Francisco' to the database hbtn_0e_100_usa using the SQLAlchemy module """ import sys from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from relationship_state import Base, State from relationship_city import City if __name__ == "__main__": engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( sys.argv[1], sys.argv[2], sys.argv[3]), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() Cali = State(name='California') SanFran = City(name='San Francisco') Cali.cities = [SanFran] session.add(Cali) session.commit()
#!/usr/bin/python3 """ Script to list all State objects from a database given """ if __name__ == "__main__": from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from relationship_state import Base, State from relationship_city import City import sys engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( sys.argv[1], sys.argv[2], sys.argv[3]), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() new_state = State() new_state.name = "California" new_city = City() new_city.name = "San Francisco" new_state.cities = [new_city] session.add(new_state) session.commit() session.close()
#!/usr/bin/python3 """Start link class to table in database """ if __name__ == "__main__": from sys import argv from relationship_state import Base, State from sqlalchemy import (create_engine) from sqlalchemy.orm import sessionmaker from relationship_city import City engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( argv[1], argv[2], argv[3]), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() state = State(name="California") statecity = City(name="San Francisco") state.cities = [statecity] session.add(state) session.commit() session.close()
#!/usr/bin/python3 """ python script """ from sys import argv from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import (create_engine) from relationship_state import Base, State from relationship_city import City from sqlalchemy.orm import sessionmaker if __name__ == "__main__": engine = create_engine('mysql+mysqldb://{}:{}@localhost:3306/{}'.format( argv[1], argv[2], argv[3]), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(engine) session = Session() new_st = State(name='California') city = City(name='San Francisco') new_st.cities = [city] session.add(new_st) session.commit() session.close()
""" prints all City objects from the database hbtn_0e_14_usa """ if __name__ == "__main__": from sys import argv from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from relationship_state import Base from relationship_state import State from relationship_city import City my_user = argv[1] my_pass = argv[2] my_db = argv[3] engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( my_user, my_pass, my_db), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() inst_state = State(name="California") inst_state.cities = [City(name="San Francsico")] session.add(inst_state) session.commit() session.close()