Skip to main content

Modify data

Overview

This guide provides instructions to modify data in a multi-index table.

Procedure

Complete the following steps to modify data in the datatable multi-index table.

1. Define The mod( ) Action

Add a mod action to the datatable multi-index table. The mod action takes as input parameters a user of type inery::name and a value of type uint32_t. The mod action updates the user object datum data member with the uint32_t value.

[[inery::action]] void mod( name user, uint32_t value );

Optionally, for ease of use add the action wrapper definition as well.

[[inery::action]] void mod( name user, uint32_t value );

+using mod_action = action_wrapper<"mod"_n, &multi_index_example::mod>;

2. Find The User You Want To Modify

Use the multi-index find( ) method to locate the user object you want to modify. The targeted user is searched based on its account name.

[[inery::action]] void multi_index_example::mod( name user, uint32_t value ) {
auto itr = datatable.find(user.value);
}

3. Submit Error If User Not Found

If the user object you want to update is not found then raise an error message by using the inery::check method.

[[inery::action]] void multi_index_example::mod( name user, uint32_t value ) {
auto itr = datatable.find(user.value);
+ check( itr != datatable.end(), "user does not exist in table" );
}

4. Update The User If Found

If the user object you want to update is found, the inery::check method will do nothing and the iterator itr will be pointing at the object which you want to update. Use the multi-index::modify( ) method to update the user object datum data member with the value parameter.

[[inery::action]] void multi_index_example::mod( name user, uint32_t value ) {
// check if the user already exists
auto itr = datatable.find(user.value);
check( itr != datatable.end(), "user does not exist in table" );

+ datatable.modify( itr, _self, [&]( auto& row ) {
+ row.datum = value;
+ });
}

Now you have implemented a new action mod. Call mod to update the datum data member for the user object identified by the user name parameter.

Summary

In conclusion, the above instructions show how to modify data in a multi-index table.