#include <eosiolib/eosio.hpp>

using namespace eosio;

class [[eosio::contract("addressbook")]] addressbook : public eosio::contract {

  using contract::contract;

  addressbook(name receiver, name code, datastream<const char*> ds): contract(receiver, code, ds) {}

  void upsert(name user, uint64_t id, std::string name, std::string x, std::string y) {
    address_index addresses(_code, _code.value);
    auto iterator = addresses.find(id);
    if( iterator == addresses.end() )
      addresses.emplace(user, [&]( auto& row ) {
       row.id = id;
       row.name = name;
       row.x = x;
       row.y = y;

  struct [[eosio::table]] names {
    uint64_t id;
    std::string name;
    std::string x;
    std::string y;
    uint64_t primary_key() const { return id; }
  typedef eosio::multi_index<"names"_n, names> address_index;


EOSIO_DISPATCH( addressbook, (upsert))

How do I search by name instead of id so I know when to create a new row? How can I make id autoincrement instead of a parameter?


  bool found = false;
  for(auto itr = addresses.begin(); itr != addresses.end() && found!=true;) {
    if(itr->did == did) {
      found = true;

  if(found == false) {
    addresses.emplace(user, [&]( auto& row ) {
    row.id = addresses.available_primary_key();
    row.did = did;
    row.x = x;
    row.y = y;

1 Answer 1


So you have to do two things:

  1. Set up name as a secondary index
  2. Get that index, which can then be used with the find command.

Unfortunately, the string type can not be used for secondary indices, only number types can be used.

Look at the examples given here: https://eosio.github.io/eosio.cdt/1.5.0/group__multiindex.html

Specifically, check out the get_index function.

  • Thanks for the response Commented Mar 12, 2019 at 23:39
  • @KaranKurbur No problem. If I have answered your question satisfactorily, you should accept and upvote my answer. This helps other people find the answer quicker Commented Mar 13, 2019 at 6:57
  • Accepted the answer thanks Commented Mar 13, 2019 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.