예제 #1
0
    def insert(self, new_record):
        '''

        :param new_record: A dictionary representing a row to add to the set of records.
        :return: None
        '''
        if not Helper.is_new_record_valid(new_record, self.get_columns()):
            self._logger.error('new_record must contains all columns')
            raise Exception

        column_strings = '('
        value_strings = '('
        for key in new_record:
            column_strings = column_strings + '`' + key + '`' + ', '
            value_strings = value_strings + '\'' + new_record[key] + '\'' + ', '

        column_strings = column_strings[:-2] + ')'
        value_strings = value_strings[:-2] + ')'

        with self._connection.cursor() as cursor:
            query = 'INSERT INTO ' + '`' + self._data[
                'table_name'] + '` ' + column_strings + ' VALUES ' + value_strings + ';'
            try:
                cursor.execute(query)
                if cursor.rowcount > 0:
                    if self._auto_commit:
                        self.commit()
            except pymysql.Error as error:
                self._logger.error(
                    'Failed to insert record(s) into the table {}'.format(
                        error))
                raise Exception
예제 #2
0
    def insert(self, new_record):
        '''

        :param new_record: A dictionary representing a row to add to the set of records.
        :return: None
        '''
        if not Helper.is_new_record_valid(new_record, self.get_columns()):
            self._logger.error('new_record must contains all columns')
            raise Exception

        if self._violate_primary_key_constraint(new_record):
            self._logger.error('Violates primary key constraint')
            raise Exception

        self._add_row(new_record)