Skip to main content

Modify Data in a Multi-Index Table

Overview

This guide provides instructions for modifying data in a multi-index table.

Prerequisites

Steps

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

1. Define the mod(...) Action

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

contract.hpp
[[sysio::action]] void mod(name user, uint32_t value);

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

contract.hpp
[[sysio::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.

contract.cpp
[[sysio::action]] void multi_index_example::mod(name user, uint32_t value) {
auto itr = testtab.find(user.value);
}

3. Yield Error If User Not Found

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

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

4. Update the User(if found)

If the user object you want to update is found, the sysio::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.

contract.cpp
[[sysio::action]] void multi_index_example::mod(name user, uint32_t value) {
// check if the user already exists
auto itr = testtab.find(user.value);
check(itr != testtab.end(), "user does not exist in table");
testtab.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.

info

A full example project demonstrating the instantiation and usage of multi-index tables can be found in the multi_index example project.

Reference

See the following code reference:

Next Steps