0

As I said I need to get the count of rows in a table. Preferly via rpc with http plugin or from a new action inside the Smart Contract.

This is my Smart Contract:

#include <eosio/eosio.hpp>

using namespace eosio;

// The contract
class [[eosio::contract("crud")]] crud : public eosio::contract {

  public:
    // Use contract's constructor
    using contract::contract;
    


    [[eosio::action]]
    void create(name user,uint64_t id, std::string data) {
        require_auth (user);
        cif_index cifrados (get_self(), get_self().value);
        auto iterator = cifrados.find(id);
        if (iterator == cifrados.end()){ 
            cifrados.emplace(get_self(), [&] (auto& row){ //Create
            row.id = id;
            row.cif = data;
        });
        }else{ 
            cifrados.modify(iterator, get_self(), [&] (auto& row){ //Update
                row.cif = data;
            });
        }
    }

    [[eosio::action]]
    void erase(name user,uint64_t id) {
        require_auth (user);

        cif_index cifrados (get_self(), get_self().value);

        auto iterator = cifrados.find(id);
        check(iterator != cifrados.end(), "Record does not exist");
        cifrados.erase(iterator);
    }


private:
  struct [[eosio::table]] cifrados
{
    uint64_t id;
    std::string cif;

    uint64_t primary_key() const { return id; }
};

  using cif_index = eosio::multi_index<"cifs"_n,cifrados>; 
};

Does someone know how to get the count? If not, it is possible to create an action in the Smart Contract to get the id of the last row? Thanks in advice

1 Answer 1

0

There is no inbuilt count()- or size()-method.

You would need to add your own counter, increasing the number of rows with each insert and decreasing it with every erase. (Theoretically you could also iterate the table and count the number of rows manually each time you need the number of rows but that's probably the most unperformant way to do this.)

You would typically just use a singleton table for this, see here for more info .

Your Answer

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

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