From 4c7e3533b281446db0b9310fb450638fcd02a203 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 3 Feb 2022 10:01:00 -0600 Subject: [PATCH] saving changes to connect SQLite database --- kennel.sql | 58 +++++++++++++++++++++ kennel.sqlite3 | Bin 0 -> 24576 bytes views/animal_requests.py | 109 ++++++++++++++++++++++++--------------- 3 files changed, 126 insertions(+), 41 deletions(-) diff --git a/kennel.sql b/kennel.sql index e69de29..1cd76ac 100644 --- a/kennel.sql +++ b/kennel.sql @@ -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); \ No newline at end of file diff --git a/kennel.sqlite3 b/kennel.sqlite3 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..c087f04f29a75d7c3972af4533a9a4b34a466ab3 100644 GIT binary patch literal 24576 zcmeI)&u-&H7y$6GV<$?sU@8`sP=!vZs;$-S(myFIAf)`MyX`hfOOlH1X){fxjp^9# zj-Br2z@du}FM)Uqt_X<>>WLE~Ci5`NcU*W-_(U zN_N2MktZD%&^7V~Q52G+ln`UnCc+UPNxl{b01BYMKOk`4(GyddjPli|0Xr1ja!!TU<(K<-v1;Y(7Om$C zC5twqaDy(Sk`24nNN!i^);+6AE4y`C*)NsoUUj>iuO84H>wxC>>$}?(sG@9D>bIeU z%N*WF)~yGSc~Brc%~~zVegAJF!fUd?_S}bXEjoHFD7v{@wYKk7!mBMbu4XoA)!MYG zR;6gwXrnZ!rm+y-w3$koPakNBsdQTT`bMwadDnKB=)dd3ZSJ4EY%v|XjTdM#rDJXO z0~U1r#(3aRat@6hE70utf#+~}9mP=dqnjKhx?0IFQE^cHfM~Sfi`$y#C$at`W%g*J zilejk4H?7BF2m@OLDMNgUUKlWt*#)hg- znR{w{YH?9H=(W{ra!`M*EM}iCCcjGpcG|%5rdVWt6y*i_3H`=R1 zwLE*Y|BbI?$_)GFs_{KBeuX#OPyhu`00mG01yBG5Pyhu`00mG01zsJ28C4^T^A+Z| z&uk$$t$CfGy|TPQ_qglQ{2501BW03ZTG$P~c-N zPUe@EGL~bDE-i7#@!afsmXkW1NxG4w7EV>!`_-D!GG^nZf8u{fDqTFl&sVR#0k z^1}AlU{t=!TejrQARL+hA`IpHFm$zKUWKcEvXm*vE^}$c7Cv{^mzU|b8*u3|K`kjg zNekkvbr@#JxxFk`tH>&GGHaTdvPWySIOCG7!nKQ%vw*i~EpWV?-#>q~RM`L5jbmc` zX8dS;XPiS4Hxxht6hHwKKmim$0Te(16hHwKK!KMNcw0}9x6)IJvcXRM9rz7^i-Pxr zOI4lL5@Z?{u1IcpUQ6&naIbd|qcI51Oe@MJ6YPkKdf1zHxEs`LSY5#9nwnM<+H}j~M*{wLzX~JzwN4Wj z67<7q1Zqap$=t2v7K1qfkc$bix}2pjQ$Og2a|LKjC-bIK=EroS(`+;8@GQjl-4oY) z?9yVOzN_lQoJ;OV+YdO*8}KBgR^TdSX1g%vPZgcazMm|!mJMqIoN~Fgp8e=9L|e8G JYXHDC{sI$0Cx-w4 literal 0 HcmV?d00001 diff --git a/views/animal_requests.py b/views/animal_requests.py index a80c985..776dc28 100644 --- a/views/animal_requests.py +++ b/views/animal_requests.py @@ -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 - \ No newline at end of file +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__) \ No newline at end of file