Archived
completed adding get_all method to get all resources from SQLite database
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
|
import sqlite3
|
||||||
|
import json
|
||||||
|
|
||||||
def get_all_customers():
|
def get_all_customers():
|
||||||
# Open a connection to the database
|
# Open a connection to the database
|
||||||
with sqlite3.connect("./kennel.sqlite3") as conn:
|
with sqlite3.connect("./kennel.sqlite3") as conn:
|
||||||
@@ -14,11 +17,11 @@ def get_all_customers():
|
|||||||
c.address,
|
c.address,
|
||||||
c.email,
|
c.email,
|
||||||
c.password,
|
c.password,
|
||||||
FROM animal a
|
FROM customer c
|
||||||
""")
|
""")
|
||||||
|
|
||||||
# Initialize an empty list to hold all animal representations
|
# Initialize an empty list to hold all customer representations
|
||||||
animals = []
|
customers = []
|
||||||
|
|
||||||
# Convert rows of data into a Python list
|
# Convert rows of data into a Python list
|
||||||
dataset = db_cursor.fetchall()
|
dataset = db_cursor.fetchall()
|
||||||
@@ -26,18 +29,18 @@ def get_all_customers():
|
|||||||
# Iterate list of data returned from database
|
# Iterate list of data returned from database
|
||||||
for row in dataset:
|
for row in dataset:
|
||||||
|
|
||||||
# Create an animal instance from the current row.
|
# Create an customer instance from the current row.
|
||||||
# Note that the database fields are specified in
|
# Note that the database fields are specified in
|
||||||
# exact order of the parameters defined in the
|
# exact order of the parameters defined in the
|
||||||
# Animal class above.
|
# Animal class above.
|
||||||
animal = Animal(row['id'], row['name'], row['breed'],
|
customer = Customer(row['id'], row['name'], row['breed'],
|
||||||
row['status'], row['location_id'],
|
row['status'], row['location_id'],
|
||||||
row['customer_id'])
|
row['customer_id'])
|
||||||
|
|
||||||
animals.append(animal.__dict__)
|
customers.append(customer.__dict__)
|
||||||
|
|
||||||
# Use `json` package to properly serialize list as JSON
|
# Use `json` package to properly serialize list as JSON
|
||||||
return json.dumps(animals)
|
return json.dumps(customers)
|
||||||
|
|
||||||
|
|
||||||
def get_single_customer():
|
def get_single_customer():
|
||||||
|
|||||||
@@ -2,7 +2,45 @@ import sqlite3
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
def get_all_employees():
|
def get_all_employees():
|
||||||
return EMPLOYEES
|
# Open a connection to the database
|
||||||
|
with sqlite3.connect("./kennel.sqlite3") 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
|
||||||
|
e.id,
|
||||||
|
e.name,
|
||||||
|
e.address,
|
||||||
|
e.email,
|
||||||
|
e.password,
|
||||||
|
FROM employee e
|
||||||
|
""")
|
||||||
|
|
||||||
|
# Initialize an empty list to hold all employee representations
|
||||||
|
employees = []
|
||||||
|
|
||||||
|
# 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 employee instance from the current row.
|
||||||
|
# Note that the database fields are specified in
|
||||||
|
# exact order of the parameters defined in the
|
||||||
|
# Animal class above.
|
||||||
|
employee = Employee(row['id'], row['name'], row['breed'],
|
||||||
|
row['status'], row['location_id'],
|
||||||
|
row['employee_id'])
|
||||||
|
|
||||||
|
employees.append(employee.__dict__)
|
||||||
|
|
||||||
|
# Use `json` package to properly serialize list as JSON
|
||||||
|
return json.dumps(employees)
|
||||||
|
|
||||||
def get_single_employee():
|
def get_single_employee():
|
||||||
requested_employee = None
|
requested_employee = None
|
||||||
|
|||||||
Reference in New Issue
Block a user