Preconditions
- It is assumed you already have a multi index table instance defined along with its mandatory primary index, otherwise take a look at the section How to instantiate a multi index table.
To insert data into a multi index table follow the following steps
- Make use of the multi index table iterator to find out if the data doesn't already exist
[[eosio::action]] void multi_index_example::set( name user ) {
// check if the user already exists
auto itr = testtab.find(user.value);
}
- Use the
emplace
method to make the insertion if the user is not already in table
[[eosio::action]] void multi_index_example::set( name user ) {
// check if the user already exists
auto itr = testtab.find(user.value);
+ if ( itr == testtab.end() ) {
+ testtab.emplace( _self, [&]( auto& u ) {
+ u.test_primary = user;
+ u.secondary = "second"_n;
+ u.datum = 0;
+ });
+ }
}