def begin_import(request_type): print '[*] Begin import' csv_file = open(csv_file_name, "rb") table = csv.DictReader(csv_file) try: session = Session() session_row = FlurryAppMetric() for row in table: if request_type == "month": # Ignore time zone as the current table does not support it session_row.date = parse(row["dateTime"]).replace(tzinfo=None) session_row.api_key = row["app|apiKey"] session_row.country = row["country|iso"] session_row.active_users_month = row["activeDevices"] try: session.merge(session_row) session.commit() except SQLAlchemyError as e: print '[*] Failed to merge!' + str(e) except SQLAlchemyError as e: print '[*] SQLAlchemyError thrown: ' + str(e) session.rollback() session.close() finally: session.close() csv_file.close()