-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_sqlite.py
323 lines (280 loc) · 9.58 KB
/
connect_sqlite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import sqlite3
import pandas as pd
# Connects to an existing database file in the current directory
# If the file does not exist, it creates it in the current directory
db_connect = sqlite3.connect('test.db')
# Instantiate cursor object for executing queries
cursor = db_connect.cursor()
# RELATION 1
# String variable for passing queries to cursor
query = """
CREATE TABLE IF NOT EXISTS Clinic (
clinicNo INTEGER PRIMARY KEY,
name TEXT,
address TEXT,
telephone TEXT )
"""
# Execute query, the result is stored in cursor
cursor.execute(query)
# Insert row into table
query = """
INSERT INTO Clinic (clinicNo, name, address, telephone)
VALUES (1, 'Pawsome Clinic 1', '123 Main St', '555-1234')
"""
cursor.execute(query)
query = """
INSERT INTO Clinic (clinicNo, name, address, telephone)
VALUES (2, 'Pawsome Clinic 2', '456 Elm St', '555-5678')
"""
cursor.execute(query)
query = """
INSERT INTO Clinic (clinicNo, name, address, telephone)
VALUES (3, 'Pawsome Clinic 3', '789 Oak St', '555-9876')
"""
cursor.execute(query)
query = """
INSERT INTO Clinic (clinicNo, name, address, telephone)
VALUES (4, 'Pawsome Clinic 4', '321 Pine St', '555-4321')
"""
cursor.execute(query)
query = """
INSERT INTO Clinic (clinicNo, name, address, telephone)
VALUES (5, 'Pawsome Clinic 5', '654 Cedar St', '555-8765')
"""
cursor.execute(query)
# Select data
query = """
SELECT name, address, telephone FROM Clinic;
"""
cursor.execute(query)
# Extract column names from cursor
column_names = [row[0] for row in cursor.description]
# Fetch data and load into a pandas dataframe
table_data = cursor.fetchall()
# RELATION 2
# String variable for passing queries to cursor
query = """
CREATE TABLE IF NOT EXISTS Staff (
staffNo INTEGER PRIMARY KEY,
name TEXT,
address TEXT,
telephone TEXT,
DOB DATE,
position TEXT,
salary REAL,
clinicNo INTEGER,
FOREIGN KEY (clinicNo) REFERENCES Clinic(clinicNo)
)
"""
# Execute query, the result is stored in cursor
cursor.execute(query)
# Insert row into table
query = """
INSERT INTO Staff (staffNo, name, address, telephone, DOB, position, salary, clinicNo)
VALUES (1, 'John Smith', '123 Main St', '555-1111', '1980-01-01', 'Veterinarian', 50000, 1)
"""
cursor.execute(query)
query = """
INSERT INTO Staff (staffNo, name, address, telephone, DOB, position, salary, clinicNo)
VALUES (2, 'Jane Doe', '456 Elm St', '555-2222', '1990-05-10', 'Veterinary Assistant', 30000, 2)
"""
cursor.execute(query)
query = """
INSERT INTO Staff (staffNo, name, address, telephone, DOB, position, salary, clinicNo)
VALUES (3, 'Robert Johnson', '789 Oak St', '555-3333', '1985-09-15', 'Receptionist', 25000, 3)
"""
cursor.execute(query)
query = """
INSERT INTO Staff (staffNo, name, address, telephone, DOB, position, salary, clinicNo)
VALUES (4, 'Emily Williams', '321 Pine St', '555-4444', '1988-07-20', 'Veterinarian', 55000, 4)
"""
cursor.execute(query)
query = """
INSERT INTO Staff (staffNo, name, address, telephone, DOB, position, salary, clinicNo)
VALUES (5, 'Michael Brown', '654 Cedar St', '555-5555', '1995-03-05', 'Veterinary Assistant', 32000, 5)
"""
cursor.execute(query)
# Select data
query = """
SELECT name, position, salary FROM Staff;
"""
cursor.execute(query)
# Extract column names from cursor
column_names2 = [row[0] for row in cursor.description]
table_data2 = cursor.fetchall()
# RELATION 3
# String variable for passing queries to cursor
query = """
CREATE TABLE IF NOT EXISTS Owner (
ownerNo INTEGER PRIMARY KEY,
name TEXT,
address TEXT,
telephone TEXT
)
"""
# Execute query, the result is stored in cursor
cursor.execute(query)
# Insert row into table
query = """
INSERT INTO Owner (ownerNo, name, address, telephone)
VALUES (1, 'David Johnson', '1234 Park Ave', '555-111')
"""
cursor.execute(query)
query = """
INSERT INTO Owner (ownerNo, name, address, telephone)
VALUES (2, 'Jennifer Smith', '5678 Lake St', '555-222')
"""
cursor.execute(query)
query = """
INSERT INTO Owner (ownerNo, name, address, telephone)
VALUES (3, 'Michelle Davis', '9876 River Rd', '555-333')
"""
cursor.execute(query)
query = """
INSERT INTO Owner (ownerNo, name, address, telephone)
VALUES (4, 'Brian Wilson', '4321 Ocean Blvd', '555-444')
"""
cursor.execute(query)
query = """
INSERT INTO Owner (ownerNo, name, address, telephone)
VALUES (5, 'Emily Turner', '8765 Mountain Ave', '555-555')
"""
cursor.execute(query)
# Select data
query = """
SELECT name, address, telephone FROM Owner;
"""
cursor.execute(query)
# Extract column names from cursor
column_names3 = [row[0] for row in cursor.description]
table_data3 = cursor.fetchall()
# RELATION 4
# String variable for passing queries to cursor
query = """
CREATE TABLE IF NOT EXISTS Pet (
petNo INTEGER PRIMARY KEY,
name TEXT,
DOB DATE,
species TEXT,
breed TEXT,
color TEXT,
ownerNo INTEGER,
clinicNo INTEGER,
FOREIGN KEY (ownerNo) REFERENCES Owner(ownerNo),
FOREIGN KEY (clinicNo) REFERENCES Clinic(clinicNo)
)
"""
# Execute query, the result is stored in cursor
cursor.execute(query)
# Insert row into table
query = """
INSERT INTO Pet (petNo, name, DOB, species, breed, color, ownerNo, clinicNo)
VALUES (1, 'Buddy', '2018-05-15', 'Dog', 'Labrador', 'Golden', 1, 1)
"""
cursor.execute(query)
query = """
INSERT INTO Pet (petNo, name, DOB, species, breed, color, ownerNo, clinicNo)
VALUES (2, 'Luna', '2019-07-10', 'Cat', 'Siamese', 'White', 2, 2)
"""
cursor.execute(query)
query = """
INSERT INTO Pet (petNo, name, DOB, species, breed, color, ownerNo, clinicNo)
VALUES (3, 'Max', '2020-03-22', 'Dog', 'German Shepherd', 'Black and Tan', 3, 3)
"""
cursor.execute(query)
query = """
INSERT INTO Pet (petNo, name, DOB, species, breed, color, ownerNo, clinicNo)
VALUES (4, 'Charlie', '2017-11-05', 'Dog', 'Golden Retriever', 'Golden', 4, 4)
"""
cursor.execute(query)
query = """
INSERT INTO Pet (petNo, name, DOB, species, breed, color, ownerNo, clinicNo)
VALUES (5, 'Bella', '2016-09-18', 'Cat', 'Persian', 'Gray', 5, 5)
"""
cursor.execute(query)
# Select data
query = """
SELECT name, species, breed, color FROM Pet;
"""
cursor.execute(query)
# Extract column names from cursor
column_names4 = [row[0] for row in cursor.description]
table_data4 = cursor.fetchall()
# RELATION 5
# String variable for passing queries to cursor
query = """
CREATE TABLE IF NOT EXISTS Examination (
examNo INTEGER PRIMARY KEY,
chiefComplaint TEXT,
description TEXT,
dateSeen DATE,
actionsTaken TEXT,
clinicNo INTEGER,
petNo INTEGER,
FOREIGN KEY (clinicNo) REFERENCES Clinic(clinicNo),
FOREIGN KEY (petNo) REFERENCES Pet(petNo)
)
"""
# Execute query, the result is stored in cursor
cursor.execute(query)
# Insert row into table
query = """
INSERT INTO Examination (examNo, chiefComplaint, description, dateSeen, actionsTaken, clinicNo, petNo)
VALUES (1, 'Fever', 'Physical examination', '2022-01-05', 'Prescribed antibiotics', 1, 1)
"""
cursor.execute(query)
query = """
INSERT INTO Examination (examNo, chiefComplaint, description, dateSeen, actionsTaken, clinicNo, petNo)
VALUES (2, 'Sore Throat', 'Strep Test', '2022-03-12', 'Rest and pain medication', 2, 2)
"""
cursor.execute(query)
query = """
INSERT INTO Examination (examNo, chiefComplaint, description, dateSeen, actionsTaken, clinicNo, petNo)
VALUES (3, 'Vomiting', 'Temperature Taken', '2022-05-20', 'Prescribed special diet and medication', 3, 3)
"""
cursor.execute(query)
query = """
INSERT INTO Examination (examNo, chiefComplaint, description, dateSeen, actionsTaken, clinicNo, petNo)
VALUES (4, 'Physical', 'Physical testing conducted', '2022-07-08', 'Physical test passed', 4, 4)
"""
cursor.execute(query)
query = """
INSERT INTO Examination (examNo, chiefComplaint, description, dateSeen, actionsTaken, clinicNo, petNo)
VALUES (5, 'Weight Loss', 'Blood work taken and physical exam', '2022-09-15', 'Referred to nutritionist', 5, 5)
"""
cursor.execute(query)
# Select data
query = """
SELECT chiefComplaint, description, dateSeen FROM Examination;
"""
cursor.execute(query)
# Extract column names from cursor
column_names5 = [row[0] for row in cursor.description]
# Fetch data and load into a pandas dataframe
table_data5 = cursor.fetchall()
df = pd.DataFrame(table_data, columns=column_names)
df2 = pd.DataFrame(table_data2, columns=column_names2)
df3 = pd.DataFrame(table_data3, columns=column_names3)
df4 = pd.DataFrame(table_data4, columns=column_names4)
df5 = pd.DataFrame(table_data5, columns=column_names5)
# Examine dataframe
print(df)
print(df.columns)
print("\n")
print(df2)
print(df2.columns)
print(df3)
print(df3.columns)
print("\n")
print(df4)
print(df4.columns)
print("\n")
print(df5)
print(df5.columns)
# Example to extract a specific column
# print(df['name'])
# Commit any changes to the database
db_connect.commit()
# Close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
db_connect.close()