-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsycopg_demo.py
132 lines (111 loc) · 4.46 KB
/
psycopg_demo.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
import psycopg2
CONNECTION_PARAMETERS = {
'dbname': 'psycopg_test_db',
'user': 'psycopg_test_user',
'password': 'password',
}
# with psycopg2.connect(**CONNECTION_PARAMETERS) as conn:
# print(conn.get_dsn_parameters())
# # Output: {'user': 'psycopg_test_user', 'dbname': 'psycopg_test_db', ...}
# with psycopg2.connect(**CONNECTION_PARAMETERS) as conn:
# with conn.cursor() as curs:
# curs.execute("SELECT USER;")
# result = curs.fetchone()
# print(result) # 'psycopg_test_user'
# with psycopg2.connect(**CONNECTION_PARAMETERS) as conn:
# with conn.cursor() as curs:
# curs.execute('SELECT manu_year, make, model FROM cars;')
# cars = curs.fetchall()
# for car in cars:
# print(car) # (1993, 'Mazda', 'Rx7')
def print_all_cars():
with psycopg2.connect(**CONNECTION_PARAMETERS) as conn:
with conn.cursor() as curs:
curs.execute('SELECT manu_year, make, model, owner_id FROM cars;')
cars = curs.fetchall()
for car in cars:
print(car)
print_all_cars()
# Output:
# (1993, 'Mazda', 'Rx7', 1)
# ...additional cars
# def get_owners_cars(owner_id):
# """
# Fetch and return all cars in the cars table
# :param owner_id: <int> the id of the owner who's cars to return
# :return: <list> the results of the query
# """
# with psycopg2.connect(**CONNECTION_PARAMETERS) as conn:
# with conn.cursor() as curs:
# curs.execute("""
# SELECT manu_year, make, model FROM cars
# WHERE owner_id = %(owner_id)s
# """,
# {'owner_id': owner_id})
# results = curs.fetchall()
# return results
# print(get_owners_cars(1)) # [(1993, 'Mazda', 'Rx7')]
# def add_new_car(manu_year, make, model, owner_id):
# """
# Add the given car to the database
# :param manu_year: <int> the year the car was made
# :param make: <string> the manufacturer of the car
# :param model: <string> the model of the car
# :param owner_id: <int> the id number of the owner
# """
# with psycopg2.connect(**CONNECTION_PARAMETERS) as conn:
# with conn.cursor() as curs:
# # curs.execute(f'INSERT INTO {table}{columns} VALUES{values};')
# curs.execute("""
# INSERT INTO cars (manu_year, make, model, owner_id)
# VALUES (%(manu_year)s, %(make)s,
# %(model)s, %(owner_id)s)
# """,
# {'manu_year': manu_year,
# 'make': make,
# 'model': model,
# 'owner_id': owner_id})
# add_new_car(2000, 'Ford', 'Lightning', 2)
# add_new_car(1994, 'Toyota', 'Supra', 2)
# print_all_cars()
# # Output:
# # ...additional cars
# # (2000, 'Ford', 'Lightning', 2)
# # (1994, 'Toyota', 'Supra', 2)
# def change_car_owner(car_id, new_owner_id):
# """
# Update the owner of a car, both by record id
# :param car_id: <int> the id of the car to change
# :param new_owner_id: <int> the owner_id to give ownership to
# """
# with psycopg2.connect(**CONNECTION_PARAMETERS) as conn:
# with conn.cursor() as curs:
# curs.execute("""
# UPDATE cars SET owner_id = %(new_owner_id)s
# WHERE id = %(car_id)s
# """,
# {'car_id': car_id,
# 'new_owner_id': new_owner_id})
# change_car_owner(5, 1)
# print_all_cars()
# # Output:
# # ...additional cars
# # (1994, 'Toyota', 'Supra', 1) <- Owner is now 1
def delete_car(car_id):
"""
Delete the record for a car given an id for that car
:param car_id: <int> the id of the car record to remove
"""
with psycopg2.connect(**CONNECTION_PARAMETERS) as conn:
with conn.cursor() as curs:
curs.execute("""
DELETE FROM cars WHERE id = %(car_id)s
""",
{'car_id': car_id})
delete_car(2)
print_all_cars()
# Output:
# (1993, 'Mazda', 'Rx7', 1)
# (1994, 'Acura', 'Integra', 3)
# (2000, 'Ford', 'Lightning', 2)
# (1994, 'Toyota', 'Supra', 1)