Created
July 31, 2020 17:25
-
-
Save CodeDrome/8c0258bac7924295199078cf83b03753 to your computer and use it in GitHub Desktop.
pgdml.py Part 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment