generated from Code-Institute-Org/ci-full-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_crud.py
155 lines (127 loc) · 3.92 KB
/
sql_crud.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
from sqlalchemy import (
create_engine, Column, Integer, String
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# executing the instructions from the "chinook" database
db = create_engine("postgresql:///chinook")
base = declarative_base()
# create a class-based model for the "Programmer" table
class Programmer(base):
__tablename__ = "Programmer"
id = Column(Integer, primary_key=True)
first_name = Column(String)
last_name = Column(String)
gender = Column(String)
nationality = Column(String)
famous_for = Column(String)
# instead of connecting to the database directly, we will ask for a session
# create a new instance of sessionmaker, then point to our engine (the db)
Session = sessionmaker(db)
# opens an actual session by calling the Session() subclass defined above
session = Session()
# creating the database using declarative_base subclass
base.metadata.create_all(db)
# creating records on our Programmer table
# ada_lovelace = Programmer(
# first_name="Ada",
# last_name="Lovelace",
# gender="F",
# nationality="British",
# famous_for="First Programmer",
# )
# alan_turing = Programmer(
# first_name="Alan",
# last_name="Turing",
# gender="M",
# nationality="British",
# famous_for="Modern Computing"
# )
# grace_hopper = Programmer(
# first_name="Grace",
# last_name="Hopper",
# gender="F",
# nationality="American",
# famous_for="COBOL language"
# )
# margaret_hamilton = Programmer(
# first_name="Margaret",
# last_name="Hamilton",
# gender="F",
# nationality="American",
# famous_for="Apollo 11"
# )
# bill_gates = Programmer(
# first_name="Bill",
# last_name="Gates",
# gender="M",
# nationality="American",
# famous_for="Microsoft"
# )
# tim_berners_lee = Programmer(
# first_name="Tim",
# last_name="Berners-Lee",
# gender="M",
# nationality="British",
# famous_for="World Wide Web"
# )
kate_mcguane = Programmer(
first_name="Kate",
last_name="McGuane",
gender="F",
nationality="Irish",
famous_for="Ergonomic Mugs"
)
# add each instance of our programmers to our session
# session.add(grace_hopper)
# session.add(margaret_hamilton)
# session.add(bill_gates)
# session.add(tim_berners_lee)
# session.add(kate_mcguane)
# commit our session to the database
# session.commit()
# updating a single record
# programmer = session.query(Programmer).filter_by(id=20).first()
# programmer.famous_for = "World President"
# updating multiple records
# people = session.query(Programmer)
# for person in people:
# if person.gender =="F":
# person.gender = "Female"
# elif person.gender == "M":
# person.gender = "Male"
# else:
# print("Gender not defined")
# session.commit()
# deleting a single record
# fname = input("Enter a first name:")
# lname = input("Enter a last name:")
# programmer = session.query(Programmer).filter_by(first_name=fname, last_name=lname).first()
# # defensive programming
# if programmer is not None:
# print("Programmer Found: ", programmer.first_name + " " + programmer.last_name)
# confirmation = input("Are you sure you want to delete this record? (y/n) ")
# if confirmation .lower() == "y":
# session.delete(programmer)
# session.commit()
# print("Programmer has been deleted")
# else:
# print("Programmer not deleted")
# else:
# print("No records found")
# delete multiple records
# programmers = session.query(Programmer)
# for programmer in programmers:
# session.delete(programmer)
# session.commit()
# query the database to find all Programmers
programmers = session.query(Programmer)
for programmer in programmers:
print(
programmer.id,
programmer.first_name + " " + programmer.last_name,
programmer.gender,
programmer.nationality,
programmer.famous_for,
sep=" | "
)