Archived
saving changes to connect SQLite database
This commit is contained in:
+58
@@ -0,0 +1,58 @@
|
||||
CREATE TABLE `Location` (
|
||||
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
`name` TEXT NOT NULL,
|
||||
`address` TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE `Customer` (
|
||||
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
`name` TEXT NOT NULL,
|
||||
`address` TEXT NOT NULL,
|
||||
`email` TEXT NOT NULL,
|
||||
`password` TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE `Animal` (
|
||||
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
`name` TEXT NOT NULL,
|
||||
`status` TEXT NOT NULL,
|
||||
`breed` TEXT NOT NULL,
|
||||
`customer_id` INTEGER NOT NULL,
|
||||
`location_id` INTEGER,
|
||||
FOREIGN KEY(`customer_id`) REFERENCES `Customer`(`id`),
|
||||
FOREIGN KEY(`location_id`) REFERENCES `Location`(`id`)
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE `Employee` (
|
||||
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
`name` TEXT NOT NULL,
|
||||
`address` TEXT NOT NULL,
|
||||
`location_id` INTEGER NOT NULL,
|
||||
FOREIGN KEY(`location_id`) REFERENCES `Location`(`id`)
|
||||
|
||||
);
|
||||
|
||||
INSERT INTO `Location` VALUES (null, 'Nashville North', "64 Washington Heights");
|
||||
INSERT INTO `Location` VALUES (null, 'Nashville South', "101 Penn Ave");
|
||||
|
||||
|
||||
INSERT INTO `Employee` VALUES (null, "Madi Peper", "35498 Madison Ave", 1);
|
||||
INSERT INTO `Employee` VALUES (null, "Kristen Norris", "100 Main St", 1);
|
||||
INSERT INTO `Employee` VALUES (null, "Meg Ducharme", "404 Unknown Ct", 2);
|
||||
INSERT INTO `Employee` VALUES (null, "Hannah Hall", "204 Empty Ave", 1);
|
||||
INSERT INTO `Employee` VALUES (null, "Leah Hoefling", "200 Success Way", 2);
|
||||
|
||||
|
||||
INSERT INTO `Customer` VALUES (null, "Mo Silvera", "201 Created St", "mo@silvera.com", "password");
|
||||
INSERT INTO `Customer` VALUES (null, "Bryan Nilsen", "500 Internal Error Blvd", "bryan@nilsen.com", "password");
|
||||
INSERT INTO `Customer` VALUES (null, "Jenna Solis", "301 Redirect Ave", "jenna@solis.com", "password");
|
||||
INSERT INTO `Customer` VALUES (null, "Emily Lemmon", "454 Mulberry Way", "emily@lemmon.com", "password");
|
||||
|
||||
|
||||
|
||||
INSERT INTO `Animal` VALUES (null, "Snickers", "Recreation", "Dalmation", 4, 1);
|
||||
INSERT INTO `Animal` VALUES (null, "Jax", "Treatment", "Beagle", 1, 1);
|
||||
INSERT INTO `Animal` VALUES (null, "Falafel", "Treatment", "Siamese", 4, 2);
|
||||
INSERT INTO `Animal` VALUES (null, "Doodles", "Kennel", "Poodle", 3, 1);
|
||||
INSERT INTO `Animal` VALUES (null, "Daps", "Kennel", "Boxer", 2, 2);
|
||||
Binary file not shown.
+68
-41
@@ -1,48 +1,48 @@
|
||||
ANIMALS = [
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Snickers",
|
||||
"species": "Dog",
|
||||
"locationId": 1,
|
||||
"customerId": 4,
|
||||
"status": "Admitted"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "Gypsy",
|
||||
"species": "Dog",
|
||||
"locationId": 1,
|
||||
"customerId": 2,
|
||||
"status": "Admitted"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"name": "Blue",
|
||||
"species": "Cat",
|
||||
"locationId": 2,
|
||||
"customerId": 1,
|
||||
"status": "Admitted"
|
||||
}
|
||||
]
|
||||
|
||||
import sqlite3
|
||||
import json
|
||||
from models import Animal
|
||||
|
||||
def get_all_animals():
|
||||
return ANIMALS
|
||||
# Open a connection to the database
|
||||
with sqlite3.connect("./kennel.sqlite3") as conn:
|
||||
|
||||
# Function with a single parameter
|
||||
def get_single_animal(id):
|
||||
# Variable to hold the found animal, if it exists
|
||||
requested_animal = None
|
||||
# Just use these. It's a Black Box.
|
||||
conn.row_factory = sqlite3.Row
|
||||
db_cursor = conn.cursor()
|
||||
|
||||
# Iterate the ANIMALS list above. Very similar to the
|
||||
# for..of loops you used in JavaScript.
|
||||
for animal in ANIMALS:
|
||||
# Dictionaries in Python use [] notation to find a key
|
||||
# instead of the dot notation that JavaScript used.
|
||||
if animal["id"] == id:
|
||||
requested_animal = animal
|
||||
# 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
|
||||
FROM animal a
|
||||
""")
|
||||
|
||||
return requested_animal
|
||||
# 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
|
||||
# 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['customer_id'])
|
||||
|
||||
animals.append(animal.__dict__)
|
||||
|
||||
# Use `json` package to properly serialize list as JSON
|
||||
return json.dumps(animals)
|
||||
|
||||
def create_animal(animal):
|
||||
# Get the id value of the last animal in the list
|
||||
@@ -85,4 +85,31 @@ def update_animal(id, new_animal):
|
||||
break
|
||||
|
||||
|
||||
|
||||
def get_single_animal(id):
|
||||
with sqlite3.connect("./kennel.sqlite3") 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
|
||||
FROM animal a
|
||||
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'])
|
||||
|
||||
return json.dumps(animal.__dict__)
|
||||
Reference in New Issue
Block a user