How to insert data into a multi index table

Preconditions

To insert data into a multi index table follow the following steps

  1. 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);

}
  1. 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;
+    });
+  }
}
Full example location

A full example project demonstrating the instantiation and usage of multi index table can be found here.