Skip to content

Instantly share code, notes, and snippets.

@CodeDrome
Created July 31, 2020 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeDrome/8c0258bac7924295199078cf83b03753 to your computer and use it in GitHub Desktop.
Save CodeDrome/8c0258bac7924295199078cf83b03753 to your computer and use it in GitHub Desktop.
pgdml.py Part 6
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