Exemplo n.º 1
0
def get_all_animals():
    # Open a connection to the database
    with sqlite3.connect("./kennel.db") as conn:

        # Just use these. It's a Black Box.
        conn.row_factory = sqlite3.Row
        db_cursor = conn.cursor()

        # Write the SQL query to get the information you want
        db_cursor.execute("""
        SELECT
        a.id,
        a.name,
        a.breed,
        a.status,
        a.location_id,
        a.customer_id,
        l.name location_name,
        l.address location_address,
        c.name customer_name,
        c.address customer_address,
        c.email customer_email,
        c.password customer_password
        FROM Animal a
        JOIN Location l
        ON l.id = a.location_id
        JOIN Customer c
        ON c.id = a.customer_id
        """)

        # Initialize an empty list to hold all animal representations
        animals = []

        # Convert rows of data into a Python list
        dataset = db_cursor.fetchall()

        # Iterate list of data returned from database
        for row in dataset:

            # Create an animal instance from the current row
            animal = Animal(row['id'], row['name'], row['breed'],
                            row['status'], row['location_id'],
                            row['customer_id'])

            # Create a Location instance from the current row
            location = Location(row['location_id'], row['location_name'],
                                row['location_address'])
            customer = Customer(row['customer_id'], row['customer_name'],
                                row['customer_address'], row['customer_email'],
                                row['customer_password'])

            # Add the dictionary representation of the location to the animal
            animal.location = location.__dict__
            animal.customer = customer.__dict__

            # Add the dictionary representation of the animal to the list
            animals.append(animal.__dict__)

    # Use `json` package to properly serialize list as JSON
    return json.dumps(animals)
Exemplo n.º 2
0
def get_all_animals():
    # Open a connection to the database
    with sqlite3.connect("./kennel.db") as conn:

        # Just use these. It's a black box
        conn.row_factory = sqlite3.Row
        db_cursor = conn.cursor()

        # Write the SQL query to get the info you want
        db_cursor.execute("""
            SELECT
                a.id,
                a.name,
                a.breed,
                a.status,
                a.location_id,
                a.treatment,
                a.customer_id,
                l.name location_name,
                l.address location_address,
                c.name customer_name,
                c.address customer_address,
                c.email customer_email,
                c.password customer_password
            FROM animal a
            JOIN location l
                ON l.id = a.location_id
            JOIN customer c
                ON c.id = a.customer_id
        """)

        # Initialize an empty list to hold all animal representations 
        animals = []

        # Convert rows of data into a Python list
        dataset = db_cursor.fetchall()

        # Iterate list of data returned from database
        for row in dataset:

            # Create an animal instance from the current row.
            # Note that the database fields are specified in
            # the exact order of the parameters defined in the
            # Animal class above
            animal = Animal(row['id'], row['name'], row['breed'], row['status'], row['location_id'], row['treatment'], row['customer_id'])

            location = Locations(row['location_id'], row['location_name'], row['location_address'])

            customer = Customer(row['customer_id'], row['customer_name'], row['customer_address'], row['customer_email'], row['customer_password'])

            animal.location = location.__dict__

            animal.customer = customer.__dict__

            animals.append(animal.__dict__)

        # Use json package to properly serialize list as JSON
        return json.dumps(animals)
Exemplo n.º 3
0
def get_single_animal(id):
    with sqlite3.connect("./kennel.db") as conn:
        conn.row_factory = sqlite3.Row
        db_cursor = conn.cursor()

        # Use a ? parameter to inject a variable's value
        # into the SQL statement.
        db_cursor.execute(
            """
        SELECT
            a.id,
            a.name,
            a.breed,
            a.status,
            a.location_id,
            a.customer_id,
            l.name location_name,
            l.address location_address,
            c.name customer_name,
            c.address customer_address,
            c.email customer_email,
            c.password customer_password
        FROM Animal a

        JOIN Location l
        ON l.id = a.location_id

        JOIN Customer c
        on c.id = a.customer_id
        WHERE a.id = ?
        """, (id, ))

        # Load the single result into memory
        data = db_cursor.fetchone()

        # Create an animal instance from the current row
        animal = Animal(data['id'], data['name'], data['breed'],
                        data['status'], data['location_id'],
                        data['customer_id'])

        location = Location(data['location_id'], data['location_name'],
                            data['location_address'])
        customer = Customer(data['customer_id'], data['customer_name'],
                            data['customer_address'], data['customer_email'],
                            data['customer_password'])

        # Add the dictionary representation of the location to the animal
        animal.location = location.__dict__
        animal.customer = customer.__dict__

        # Add the dictionary representation of the animal to the list
        # animals.append(animal.__dict__)

    return json.dumps(animal.__dict__)
Exemplo n.º 4
0
def get_single_animal(id):
    with sqlite3.connect("../../kennel.db") as conn:
        conn.row_factory = sqlite3.Row
        db_cursor = conn.cursor()

        # Use a ? parameter to inject a variable's value
        # into the SQL statement.
        db_cursor.execute("""
        SELECT
            a.id,
            a.name,
            a.breed,
            a.status,
            a.location_id,
            a.customer_id,
            l.name location_name,
            l.address location_address,
            c.name customer_name,
            c.address customer_address,
            c.email customer_email
        FROM animal a
        JOIN location l
            ON l.id = a.location_id
        JOIN customer c
            ON c.id = a.customer_id
        WHERE a.id = ?
        """, (id, ))

        # Load the single result into memory
        data = db_cursor.fetchone()

        # Create an animal instance from the current row
        animal = Animal(data['id'], data['name'], data['breed'],
                        data['status'], data['location_id'],
                        data['customer_id'])

        location = Location(data['location_id'],
                            data['location_name'], data['location_address'])

        # remove the location id from the results
        del location.__dict__['id']

        animal.location = location.__dict__

        customer = Customer(data['customer_id'], data['customer_name'],
                            data['customer_address'], data['customer_email'])

        # remove the customer id and password field from the results
        del customer.__dict__['id']
        del customer.__dict__['password']

        animal.customer = customer.__dict__

        return json.dumps(animal.__dict__)
def get_single_animal(id):
    with sqlite3.connect("./kennel.db") as conn:
        conn.row_factory = sqlite3.Row
        db_cursor = conn.cursor()

        # Use a ? parameter to inject a variable's value
        # into the SQL statement.
        db_cursor.execute(
            """
        SELECT
            a.id,
            a.name animal_name,
            a.status,
            a.breed,
            a.customer_id,
            a.location_id,
            c.name customer_name,
            l.name location_name,
            l.address
        FROM animal a
        JOIN Customer c ON c.id = a.customer_id
        JOIN Location l ON l.id = a.location_id
        WHERE a.id = ?
        """, (id, ))

        # Load the single result into memory
        data = db_cursor.fetchone()

        # Create an animal instance from the current row
        animal = Animal(data['id'], data['animal_name'], data['status'],
                        data['breed'], data['location_id'],
                        data['customer_id'])

        location = Location("", data['location_name'], data['address'])
        animal.location = location.__dict__

        customer = Customer("", data['customer_name'], "", "", "")
        animal.customer = customer.__dict__

        return json.dumps(animal.__dict__)
Exemplo n.º 6
0
def get_single_animal(id):
    # Open a connection to the database
    with sqlite3.connect("./kennel.db") as conn:
        conn.row_factory = sqlite3.Row
        db_cursor = conn.cursor()

        # Write the SQL query to get the information you want
        db_cursor.execute("""
        SELECT
            a.id,
            a.name animal_name,
            a.breed,
            a.status,
            a.customer_id,
            a.location_id,
            l.name location_name,
            c.name customer_name,
            c.id customer_id,
            c.address
        FROM animal a
        JOIN location l
            ON l.id = a.location_id
        JOIN customer c
            ON c.id = a.customer_id
        WHERE a.id = ?
        """, (id, ))

        data = db_cursor.fetchone()

        # Create an animal instance from the current row
        animal = Animal(data['animal_name'], data['breed'], data['status'],
                        data['location_id'], data['customer_id'], data['id'])

        location = Location(data['location_name'])
        animal.location = location.__dict__

        customer = Customer(data['customer_id'], data['customer_name'], data['address'])
        animal.customer = customer.__dict__

        return json.dumps(animal.__dict__)
Exemplo n.º 7
0
def get_all_animals():
    # Open a connection to the database
    # These three lines are how we talk to the database
    with sqlite3.connect("./kennel.db") as conn:

        # Just use these. It's a Black Box.
        conn.row_factory = sqlite3.Row
        db_cursor = conn.cursor()

        # Write the SQL query to get the information you want
        # db cursor is just letting us make a sql query
        # Telling the db what info to get and what table to get it from
        # """ allows for multi line strings
        db_cursor.execute("""
        SELECT
            a.id,
            a.name,
            a.breed,
            a.status,
            a.location_id,
            a.customer_id,
            l.name location_name,
            l.address location_address,
            c.name customer_name,
            c.address customer_address,
            c.email customer_email,
            c.password customer_password
        FROM Animal a
        JOIN Location l
            ON l.id = a.location_id
        JOIN Customer c
            ON c.id = a.customer_id
        """)

        # Initialize an empty list to hold all animal representations
        animals = []

        # Convert rows of data into a Python list
        # Grabs everything that matches the SELECT query above
        # gets the result
        dataset = db_cursor.fetchall()

        # Iterate list of data returned from database
        for row in dataset:

            # Create an animal instance from the current row.
            # Note that the database fields are specified in
            # exact order of the parameters defined in the
            # Animal class above.
            # using backet notation to grab the info off of the 'row'
            # this has to match the select above
            animal = Animal(row['id'], row['name'], row['breed'],
                            row['status'], row['location_id'],
                            row['customer_id'])

            location = Location(row['location_id'], row['location_name'],
                                row['location_address'])
            animal.location = location.__dict__

            customer = Customer(row['id'], row['customer_name'],
                                row['customer_address'], row['customer_email'],
                                row['customer_password'])
            animal.customer = customer.__dict__

            # adding animal dictionary to the animal list (like a push) above which is equal to an empty array []
            animals.append(animal.__dict__)

    # Use `json` package to properly serialize list as JSON
    return json.dumps(animals)
Exemplo n.º 8
0
def get_all_animals():
    # Open a connection to the database
    with sqlite3.connect("./kennel.db") as conn:

        # Just use these. It's a Black Box.
        conn.row_factory = sqlite3.Row
        #variable the database con.cursor() lets us talk to the database
        db_cursor = conn.cursor()

        # Write the SQL query to get the information you want
        #dbcursor tells it what sql command to execute in this case its SELECT
        db_cursor.execute("""
        SELECT
            a.id,
            a.name,
            a.breed,
            a.status,
            a.location_id,
            a.customer_id,
            l.name location_name,
            l.address location_address,
            c.id,
            c.name,
            c.address,
            c.email,
            c.password        
        FROM Animal a
        JOIN Location l
            ON l.id = a.location_id
        JOIN Customer c
            ON c.id = a.customer_id       """)

        # Initialize an empty list to hold all animal representations
        animals = []

        # Convert rows of data into a Python list
        ## dataset pretty much fetche al lthat was executed at line 65
        dataset = db_cursor.fetchall()

        # Iterate list of data returned from database
        for row in dataset:

            # Create an animal instance from the current row.
            # Note that the database fields are specified in
            # exact order of the parameters defined in the
            # Animal class above.
            ## This makes a new animal instance and we're calling it the properties on the class in the modal folder animal.py
            animal = Animal(row['id'], row['name'], row['breed'],
                            row['status'], row['location_id'],
                            row['customer_id'])

            # Create a Location instance from the current row
            location = Location(row['id'], row['location_name'],
                                row['location_address'])

            # Create a Customer instance from the current row
            customer = Customer(row['id'], row['name'], row['address'],
                                row['email'], row['password'])

            # Add the dictionary representation of the location to the animal
            animal.location = location.__dict__
            # Add the dictionary representation of the customer to the animal
            animal.customer = customer.__dict__

            # Add the dictionary representation of the animal to the list
            animals.append(animal.__dict__)

    # Add the dictionary representation of the customer to the list

    # Use `json` package to properly serialize list as JSON
    # changes from a dict to a jsonString
    return json.dumps(animals)