示例#1
0
def main():
    '''s
    script that creates the State “California”
    with the City “San Francisco” from the database

    '''

    credentials = (sys.argv[1], sys.argv[2], sys.argv[3])

    engine = create_engine(
        'mysql+mysqldb://{}:{}@localhost/{}'.format(*credentials),
        pool_pre_ping=True)

    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()

    # Define state name
    new_state_name = 'California'
    new_city_name = 'San Francisco'
    # Create new state
    new_state = State(name=new_state_name)
    new_city = City(name=new_city_name, state=new_state)
    # add new state to sessioin
    session.add(new_city)
    session.commit()
示例#2
0
def main():
    """
    creates the State “California” with the City “San Francisco”
    from the database hbtn_0e_100_usa
    """
    # Get arguments from command line
    route, username, password, database = sys.argv
    # Create engine
    engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format(
        username, password, database), pool_pre_ping=True)
    # Creates a shema
    Base.metadata.create_all(engine)
    # Creates a session Class
    Session = sessionmaker(bind=engine)
    # Create session object
    session = Session()

    # Create new records
    new_state = State(name="California")
    new_city = City(name="San Francisco")

    # Insert new_city into new_state collection
    new_state.cities.append(new_city)

    # Insert data to table sheme (memory)
    session.add(new_state)

    # Send data to DB
    session.commit()
    session.close()
示例#3
0
def get_data(engine):
    ''' Get data from database '''
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()

    row_city = City(name='San Francisco')
    row_state = State(name='California', cities=[row_city])
    session.add(row_state)
    session.commit()

    session.close()
示例#4
0
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()
示例#5
0
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_state():
    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)()
    c = City(name='San Francisco')
    s = State(name='California', cities=[c])
    session.add(s)
    session.add(c)
    session.commit()
    session.close()
def list_state():
    """list my states"""
    engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'
                           .format(argv[1], argv[2], argv[3]),
                           pool_pre_ping=True)
    Base.metadata.create_all(engine)
    session = Session(engine)
    st = State(name='California')
    ct = City(name='San Francisco')
    st.cities.append(ct)
    session.add_all([ct, st])
    session.commit()
    session.close()
def mainx():
    """ con """
    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()
    px = State(name='California')
    city = City(name='San Francisco')
    px.cities.append(city)
    session.add(px)
    session.commit()
    session.close()
def main(argv):

    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(bind=engine)

    session = Session()
    new = State(name="California")
    new_c = City(name="San Francisco")
    new.cities.append(new_c)
    session.add(new)
    session.commit()

    session.close()
def create_data():
    """
    Creates the State “California” with the City
    “San Francisco” from the database hbtn_0e_100_usa
    """
    arg = sys.argv
    url_base = "mysql+mysqldb://{}:{}@localhost:3306/{}"
    db_url = url_base.format(arg[1], arg[2], arg[3])
    engine = create_engine(db_url, pool_pre_ping=True)
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    parent = State(name='California')
    child = City(name='San Francisco')
    parent.cities.append(child)
    session.add(parent)
    session.commit()
示例#11
0
def main():
    """
    This file use a mysql search from python
    """
    db_user = sys.argv[1]
    db_password = sys.argv[2]
    db_name = sys.argv[3]
    engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format(
        db_user, db_password, db_name),
                           pool_pre_ping=True)
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    new_state = State(name="California")
    new_city = City(name="San Francisco", state=new_state)
    session.add(new_city)
    session.commit()
    session.close()
示例#12
0
def main():
    """
    Main function for the Module
    """
    warnings.simplefilter('ignore', Warning)
    cs = 'mysql+mysqldb://{}:{}@localhost/{}'.format(sys.argv[1], sys.argv[2],
                                                     sys.argv[3])
    engine = create_engine(cs, pool_pre_ping=True)
    Base.metadata.create_all(engine)
    session = Session(engine)

    new_state = State(name="California")
    new_city = City(name="San Francisco", state=new_state)
    session.add(new_city)
    session.commit()

    session.close()
    warnings.simplefilter('always', Warning)
def main():
    """ Connect and Read Data Base """
    db = 'mysql+mysqldb://{}:{}@localhost/{}'.format(sys.argv[1],
                                                     sys.argv[2],
                                                     sys.argv[3],
                                                     pool_pre_ping=True)

    engine = create_engine(db)
    Base.metadata.create_all(engine)

    session = sessionmaker(bind=engine)()

    relation = State(name='California')
    relation.cities.append(City(name='San Francisco'))
    session.add(relation)
    session.commit()

    session.close()
def print_func():
    if (len(argv) != 4):
        return

    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_city = City(name="San Francisco")
    new_city.state = State(name="California")
    session.add(new_city)
    session.commit()

    session.close()
示例#15
0
def main(argv):
    """func - main - argv"""
    if len(argv) != 4:
        print("Enter 3 arguments")
        return
    session = sessionmaker()
    engine = create_engine('mysql+mysqldb://'
                           '{}:{}@localhost:3306/{}'.format(
                               sys.argv[1], sys.argv[2], sys.argv[3]),
                           pool_pre_ping=True)
    session.configure(bind=engine)
    Base.metadata.create_all(engine)
    s = session()
    add_sc = State(name="California")
    add_ct = City(name="San Francisco")
    add_sc.cities.append(add_ct)
    s.add(add_sc)
    s.commit()
    s.close()
示例#16
0
def getAllCities(user2, passward2, db2):
    """ script that gets all the states when called on """
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker
    from relationship_city import City
    from relationship_state import Base, State

    engine = create_engine('mysql+mysqldb://{}:{}@localhost:3306/{}'.format(
        user2, passward2, db2))
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    c1 = City(name="San Francisco")
    s1 = State(name="California", cities=[c1])

    session.add(s1)
    session.add(c1)

    session.commit()
    session.close()
def main():
    """main function """

    dialect = 'mysql'
    driver = 'mysqldb'
    username = sys.argv[1]
    password = sys.argv[2]
    host = 'localhost'
    port = '3306'
    database = sys.argv[3]
    engine = create_engine("{}+{}://{}:{}@{}:{}/{}".format(
        dialect, driver, username, password, host, port, database))
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    ci = City(name="San Fransisco")
    state = State(name="California", cities=ci)
    session.add(ci)
    session.add(state)
    session.commit()
    session.close()
def create_state_cities():
    """Fetches all states"""
    username = sys.argv[1]
    password = sys.argv[2]
    database = sys.argv[3]

    engine = create_engine('mysql+mysqldb://{}:{}@localhost:3306/{}'.format(
        username, password, database),
                           pool_pre_ping=True)

    Base.metadata.create_all(engine)
    Session = sessionmaker()
    Session.configure(bind=engine)
    session = Session()

    new_state = State(name='California')
    new_city = City(name='San Francisco')
    new_state.cities.append(new_city)
    session.add(new_state)
    session.add(new_city)
    session.commit()
    session.close()
示例#19
0
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()
def main():
    """ Creates the State “California” with the City “San Francisco” from the
        database hbtn_0e_100_usa

        The basic functionality of SQLAlchemy, which is the SQL expression
        language, is used, to create some metadata that will contain a series
        of modules (or objects).

            1 Create a DB engine to establish connection, which takes 3 command
               line arguments: [1]: username, [2]: password and [3]: DB name.
            2 Use the .create_all() method on the metadata object and pass it
              the engine connection for SQLAlchemy to generate a table.
            3 Create a session to interact with the DB. I create an access
              to the Session custom class and I create an instance
              of it (without argument).
            4 Query the DB using the query() method of the session object.
    """
    engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format(
        argv[1], argv[2], argv[3]),
                           pool_pre_ping=True)
    Base.metadata.create_all(engine)
    # Sessionmaker class with creates Session class with default arguments
    # set for its constructor.
    Session = sessionmaker(bind=engine)
    session = Session()

    # Add new data and send to DB
    newDataState = State(name="California")
    newDataCity = City(name="San Francisco", state=newDataState)

    # Collection attribute append() method. (Adding a city to state)
    # newDataState.cities.append(newDataCity)
    session.add(newDataState)
    session.add(newDataCity)
    session.commit()

    session.close()
示例#21
0
if __name__ == "__main__":
    from sqlalchemy.engine import create_engine
    from sqlalchemy.engine.url import URL
    from sqlalchemy.orm import Session
    from relationship_city import City
    from relationship_state import Base, State
    from sys import argv

    db = {
        'drivername': 'mysql+mysqldb',
        'host': 'localhost',
        'port': '3306',
        'username': argv[1],
        'password': argv[2],
        'database': argv[3]
    }

    url = URL(**db)
    engine = create_engine(url, pool_pre_ping=True)
    Base.metadata.create_all(engine)

    session = Session(engine)
    city = City(name='San Francisco')
    state = State(name='California', cities=[city])

    session.add(state)
    session.add(city)
    session.commit()
    session.close()
示例#22
0
#!/usr/bin/python3
'''creates the State “California” with the City “San Francisco” '''

import sys
from relationship_state import State
from relationship_city import Base, City
from sqlalchemy import (create_engine)
from sqlalchemy.orm import sessionmaker

if __name__ == "__main__":
    dbConnector = 'mysql+mysqldb://{}:{}@localhost/{}'
    engine = create_engine(dbConnector.format(sys.argv[1], sys.argv[2],
                                              sys.argv[3]),
                           pool_pre_ping=True)
    Base.metadata.create_all(engine)
    Base.metadata.bind = engine
    DBSession = sessionmaker()
    DBSession.bind = engine
    session = DBSession()

    # create state object
    california = State(name="California")

    # now create City San francisco and assign California State
    sanFrancisco = City(name="San Francisco")
    california.cities.append(sanFrancisco)

    session.add(sanFrancisco)
    session.commit()
    session.close()
示例#23
0
#!/usr/bin/python3
"""Start link class to table 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, relationship, backref

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, checkfirst=True)
    Session = sessionmaker(bind=engine)
    session = Session()
    state = State(name="California")
    city = City(name="San Francisco")
    state.cities.append(city)
    session.add(state)
    session.commit()
    session.close()
示例#24
0
#!/usr/bin/python3
"""
script that creates the State “California” with the City “San Francisco”
from the database hbtn_0e_100_usa: (100-relationship_states_cities.py)
"""
from sqlalchemy import create_engine
from relationship_state import Base, State
from relationship_city import City
from sqlalchemy.orm import sessionmaker
from sys import argv

if __name__ == "__main__":
    engine = create_engine("mysql+mysqldb://{}:{}@localhost/{}".format(
        argv[1], argv[2], argv[3]))
    Session = sessionmaker()
    session = Session(bind=engine)
    Base.metadata.create_all(engine)
    new = State(name="California", cities=[City(name="San Francisco")])
    session.add(new)
    session.commit()
    session.close()
#!/usr/bin/python3
""" Python x SQLAlchemy  : adds data to a table """
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/{}'.format(
        argv[1], argv[2], argv[3]))
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    state = State(name="California", cities=City(name="San Francisco"))
    session.add(state)
    session.commit()
    session.close()
示例#26
0
#!/usr/bin/python3
"""
module for task 15
"""

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

if __name__ == "__main__":
    e = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format(
        argv[1], argv[2], argv[3]),
                      pool_pre_ping=True)
    Base.metadata.create_all(e)
    Session = sessionmaker(bind=e)
    session = Session()
    new_s = State(name="California")
    new_c = City(name="San Francisco")
    new_s.cities.append(new_c)
    session.add(new_s)
    session.add(new_c)
    session.commit()
    session.close()
示例#27
0
#!/usr/bin/python3
"""Start link class to table 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]))
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    s2 = Session()
    content = City(name='San Francisco', state=State(name='California'))
    s2.add(content)
    s2.commit()
    s2.close()
示例#28
0
#!/usr/bin/python3
'''
script that creates the State “California” with the City
“San Francisco” from the database hbtn_0e_100_usa
'''

import sys
from relationship_state import Base, State
from relationship_city import City
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from sqlalchemy.schema import Table

if __name__ == "__main__":

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

    Base.metadata.create_all(eng)

    session = Session(eng)

    NEW_city = City(name='San Francisco')
    NEW = State(name='California')
    new.cities.append(NEW_city)
    session.add_all([NEW, NEW_city])

    session.commit()
    session.close()
#!/usr/bin/python3
"""0x0F. Python - Object-relational mapping - task 15. City relationship
"""

if __name__ == '__main__':
    from sys import argv, exit
    from sqlalchemy import create_engine
    from sqlalchemy.orm import Session
    from relationship_state import Base, State
    from relationship_city import City

    if len(argv) != 4:
        exit('Use: 100-relationship_states_cities.py <mysql username> '
             '<mysql password> <database name>')

    engine = create_engine('mysql+mysqldb://{}:{}@localhost:3306/'
                           '{}'.format(argv[1], argv[2], argv[3]),
                           pool_pre_ping=True)
    session = Session(engine)
    Base.metadata.create_all(engine)  # creates decprecated warning 1681

    new_state = State(name='California')
    new_city = City(name='San Francisco', state_id=new_state.id)
    new_state.cities.append(new_city)
    session.add_all([new_state, new_city])
    session.commit()
    session.close()
#!/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()