def insert_event(calendar_id, recurrence, name='', description='', duration=3600): """ insert a new event into the event table """ sql = """INSERT INTO event(calendar_id,recurrence,name,description,duration) VALUES(%s,%s,%s,%s,%s) RETURNING event_id;""" conn = None event_id = None try: conn = connect() cur = conn.cursor() cur.execute(sql, (calendar_id, recurrence, name, description, duration)) # get the generated id back event_id = cur.fetchone()[0] # commit the changes to the database conn.commit() # close communication with the database cur.close() except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: conn.close() return event_id
def delete_calendar(calendar_id): """ delete calendar by calendar id """ conn = None rows_deleted = 0 try: conn = connect() # create a new cursor cur = conn.cursor() # execute the UPDATE statement cur.execute("DELETE FROM calendar WHERE calendar_id = %s", (calendar_id, )) # get the number of updated rows rows_deleted = cur.rowcount # Commit the changes to the database conn.commit() print(rows_deleted) # Close communication with the PostgreSQL database cur.close() except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: conn.close() return rows_deleted
def delete_holiday(holiday_id): """ delete holiday by holiday id """ conn = None rows_deleted = 0 try: # invoke connection to bd method(stays open) conn = connect() # create a new cursor cur = conn.cursor() # execute the UPDATE statement cur.execute("DELETE FROM holidays WHERE holiday_id = %s", (holiday_id, )) # get the number of updated rows rows_deleted = cur.rowcount # Commit the changes to the database conn.commit() # Close communication with the PostgreSQL database cur.close() except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: conn.close() return rows_deleted
def remove_allcalendars(): """ delete all calendars """ conn = None rows_deleted = 0 try: # invoke connection to bd method(stays open) conn = connect() # create a new cursor cur = conn.cursor() # execute the UPDATE statement cur.execute("DELETE FROM calendar") # get the number of updated rows rows_deleted = cur.rowcount # Commit the changes to the database conn.commit() # Close communication with the PostgreSQL database cur.close() print(rows_deleted) except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: conn.close() return rows_deleted
def insert_holiday( recurrence, name = '' , description = '', ): """ insert a new holiday into the holidays table """ sql = """INSERT INTO holidays(recurrence,name,description) VALUES(%s,%s,%s) RETURNING holiday_id;""" conn = None holiday_id = None try: conn = connect() # create a new cursor cur = conn.cursor() # execute the INSERT statement cur.execute(sql, (recurrence,name,description)) # get the generated id back holiday_id = cur.fetchone()[0] # commit the changes to the database conn.commit() # close communication with the database cur.close() except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: conn.close() return holiday_id