#!/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()
# mysql+mysqldb://user:passwd@localhost/my_db engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( sys.argv[1], sys.argv[2], sys.argv[3]), pool_pre_ping=True) # This is the function that does the SQL CREATE TABLE... # or ALTER TABLE... commands. # We passed engine (from the code above) to this function to generate # the table(s) that are mapped to objects that inherit from Base. Base.metadata.create_all(engine) # Here we are defining a class called Session and an instance of that # class called sesh. The sessionmaker lets us talk to the database in # a controlled environment. This session is bound to our database using # the engine we defined above. Session = sessionmaker(bind=engine) sesh = Session() # Relationship - Association of Objects instance_state = State() instance_state.name = "California" instance_city = City() instance_city.name = "San Francisco" instance_state.cities.append(instance_city) # Add the new elements sesh.add(instance_state) sesh.add(instance_city) # Save changes sesh.commit() # To close the connection with the db sesh.close()
#!/usr/bin/python3 """ Connecting script with my database """ from relationship_city import City from relationship_state import State, Base from sys import argv from sqlalchemy import create_engine, orm from sqlalchemy.orm import sessionmaker import sqlalchemy if __name__ == '__main__': engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format( argv[1], argv[2], 'localhost', argv[3])) Base.metadata.create_all(engine) Session = sessionmaker(engine) session = Session() new_state = State() new_city = City() new_state.name = 'California' new_city.name = 'San Francisco' new_state.cities = [new_city] session.add(new_state, new_city) session.commit() session.close()
#!/usr/bin/python3 """script that prints the first State object from the database hbtn_0e_6_usa""" from sqlalchemy import (create_engine) from relationship_state import Base, State from relationship_city import City from sqlalchemy.orm import sessionmaker import sys if __name__ == "__main__": """Connecting""" engine = create_engine('mysql+mysqldb://{}:{}@localhost:3306/{}'.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() estado = State() estado.name = 'California' ciudad = City() ciudad.name = 'San Francisco' estado.cities.append(ciudad) session.add(estado) session.add(ciudad) session.commit() session.close()