跳至主要内容
此页面是从英文翻译而来的。请注意,与原始页面相比,可能会出现错误或差异。真实的文档来源应始终是英文版本。

How to modify data in a multi-index table

Overview

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

Reference

See the following code reference:

Before you begin

Make sure you have the following prerequisites in place:

Procedure

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 eosio::name and a value of type uint32_t. The mod action updates the user object datum data member with the uint32_t value.

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

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

[[eosio::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.

[[eosio::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 object you want to update is not found then raise an error message by using the eosio::check method.

[[eosio::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 eosio::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.

[[eosio::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 | Full example location]] | A full example project demonstrating the instantiation and usage of multi-index table can be found here.

Summary

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

Next Steps