This repository was archived by the owner on Apr 20, 2026. It is now read-only.
forked from CodeDrome/postgresql-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgdml.py
More file actions
executable file
·314 lines (179 loc) · 8.1 KB
/
pgdml.py
File metadata and controls
executable file
·314 lines (179 loc) · 8.1 KB
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
import datetime
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import pgconnection
def main():
print("------------------")
print("| codedrome.com |")
print("| PostgreSQL DML |")
print("------------------\n")
insert_galleries()
insert_photos()
insert_typesdemo()
#update_photos()
#delete_photos()
#select_photos()
#select_galleriesphotos()
#insert_photo_invalid_fk()
#delete_fk()
#reset_serial()
def insert_galleries():
galleries = ({"name": "London 2018", "description": "Photos of London in 2018"},
{"name": "Paris 2016", "description": "Photos of Paris in 2016"},
{"name": "Oslo 2018", "description": "Photos of Oslo in 2018"},
{"name": "Copenhagen 2017", "description": "Photos of Copenhagen in 2017"},
{"name": "Edinburgh 2015", "description": "Photos of Edinburgh in 2015"})
try:
conn = pgconnection.get_connection("codeinpython")
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
for gallery in galleries:
cursor.execute("""INSERT INTO galleries(name, description)
VALUES (%(name)s, %(description)s);""",
{'name': gallery["name"], 'description': gallery["description"]})
print("Gallery inserted")
cursor.close()
conn.close()
except psycopg2.Error as e:
print(type(e))
print(e)
def insert_photos():
photos = ({"galleryid": 1, "title": "London Photo 1", "description": "London Photo 1", "photographer": "Chris Webb", "datetaken": datetime.date(2018, 5, 17)},
{"galleryid": 1, "title": "London Photo 2", "description": "London Photo 2", "photographer": "Chris Webb", "datetaken": datetime.date(2018, 5, 18)},
{"galleryid": 2, "title": "Paris Photo 1", "description": "Paris Photo 1", "photographer": "Chris Webb", "datetaken": datetime.date(2016, 9, 1)},
{"galleryid": 2, "title": "Paris Photo 2", "description": "Paris Photo 2", "photographer": "Chris Webb", "datetaken": datetime.date(2016, 9, 1)},
{"galleryid": 3, "title": "Oslo Photo 1", "description": "Oslo Photo 1", "photographer": "Chris Webb", "datetaken": datetime.date(2018, 7, 5)},
{"galleryid": 3, "title": "Oslo Photo 2", "description": "Oslo Photo 2", "photographer": "Chris Webb", "datetaken": datetime.date(2018, 7, 5)},
{"galleryid": 4, "title": "Copenhagen Photo 1", "description": "Copenhagen Photo 1", "photographer": "Chris Webb", "datetaken": datetime.date(2017, 4, 12)},
{"galleryid": 4, "title": "Copenhagen Photo 2", "description": "Copenhagen Photo 2", "photographer": "Chris Webb", "datetaken": datetime.date(2017, 4, 13)},
{"galleryid": 5, "title": "Edinburgh Photo 1", "description": "Edinburgh Photo 1", "photographer": "Chris Webb", "datetaken": datetime.date(2015, 8, 21)},
{"galleryid": 5, "title": "Edinburgh Photo 2", "description": "Edinburg Photo 2", "photographer": "Chris Webb", "datetaken": datetime.date(2015, 8, 21)})
try:
conn = pgconnection.get_connection("codeinpython")
cursor = conn.cursor()
for photo in photos:
cursor.execute("""INSERT INTO photos(galleryid, title, description, photographer, datetaken)
VALUES (%(galleryid)s, %(title)s, %(description)s, %(photographer)s, %(datetaken)s);""",
photo)
conn.commit()
print("Photo inserted")
cursor.close()
conn.close()
except psycopg2.Error as e:
print(type(e))
print(e)
def insert_typesdemo():
row = {"intcolumn": 123, "realcolumn": 456.789, "varcharcolumn": "Now is the winter of our discontent", "datecolumn": datetime.date(2018, 2, 17), "booleancolumn": True}
try:
conn = pgconnection.get_connection("codeinpython")
cursor = conn.cursor()
cursor.execute("""INSERT INTO typesdemo(intcolumn, realcolumn, varcharcolumn, datecolumn, booleancolumn)
VALUES (%(intcolumn)s, %(realcolumn)s, %(varcharcolumn)s, %(datecolumn)s, %(booleancolumn)s);""",
row)
conn.commit()
print("typesdemo row inserted")
cursor.close()
conn.close()
except psycopg2.Error as e:
print(type(e))
print(e)
def update_photos():
update_dict = {"description": "Edinburgh Photo 2", "photoid": 10}
try:
conn = pgconnection.get_connection("codeinpython")
cursor = conn.cursor()
cursor.execute("UPDATE photos SET description = %(description)s WHERE photoid = %(photoid)s;", update_dict)
conn.commit()
print("Photo updated")
cursor.close()
conn.close()
except psycopg2.Error as e:
print(type(e))
print(e)
def delete_photos():
delete_dict = {"photoid": 10}
try:
conn = pgconnection.get_connection("codeinpython")
cursor = conn.cursor()
cursor.execute("DELETE FROM photos WHERE photoid = %(photoid)s;", delete_dict)
conn.commit()
print("Photo deleted")
cursor.close()
conn.close()
except psycopg2.Error as e:
print(type(e))
print(e)
def select_photos():
# Wildcards:
# percent sign % for 0 or more characters
# underscore _ for exactly 1 character
select_dict = {"where_like": '%Oslo%'}
try:
conn = pgconnection.get_connection("codeinpython")
cursor = conn.cursor()
# To make LIKE case insensitive use ILIKE
cursor.execute("SELECT galleryid, title, description, photographer, datetaken FROM photos WHERE description LIKE %(where_like)s;", select_dict)
# get a list of tuples containing the data
data = cursor.fetchall()
cursor.close()
conn.close()
for row in data:
print(row)
except psycopg2.Error as e:
print(type(e))
print(e)
def select_galleriesphotos():
try:
conn = pgconnection.get_connection("codeinpython")
cursor = conn.cursor()
cursor.execute("SELECT galleryname, gallerydescription, phototitle, photodescription FROM galleriesphotos")
# get a list of tuples containing the data
data = cursor.fetchall()
cursor.close()
conn.close()
for row in data:
print(row)
except psycopg2.Error as e:
print(type(e))
print(e)
def insert_photo_invalid_fk():
invalid_photo = {"galleryid": 6, "title": "Hong Kong Photo 1", "description": "Hong Kong Photo 1", "photographer": "Chris Webb", "datetaken": datetime.date(2018, 8, 27)}
try:
conn = pgconnection.get_connection("codeinpython")
cursor = conn.cursor()
cursor.execute("""INSERT INTO photos(galleryid, title, description, photographer, datetaken)
VALUES (%(galleryid)s, %(title)s, %(description)s, %(photographer)s, %(datetaken)s);""",
invalid_photo)
conn.commit()
print("Photo inserted")
cursor.close()
conn.close()
except psycopg2.Error as e:
print(type(e))
print(e)
def delete_fk():
delete_dict = {"galleryid": 1}
try:
conn = pgconnection.get_connection("codeinpython")
cursor = conn.cursor()
cursor.execute("DELETE FROM galleries WHERE galleryid = %(galleryid)s;", delete_dict)
conn.commit()
print("Gallery deleted")
cursor.close()
conn.close()
except psycopg2.Error as e:
print(type(e))
print(e)
def reset_serial():
try:
conn = pgconnection.get_connection("codeinpython")
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
cursor.execute("ALTER SEQUENCE photos_photoid_seq RESTART WITH 1")
print("photo table serial reset")
cursor.close()
conn.close()
except psycopg2.Error as e:
print(type(e))
print(e)
main()