0

How can I use a uuid as the primary.

struct [[eosio::table]] blackboxdata {
    name           user;
    std::string    uuid;
    std::string    hash;
    std::string    sqlrequest;
    uint64_t       primary_key() const { return uuid;}
  };

error : no viable conversion from returned value of type 'const std::string' (aka 'const basic_string, allocator >') to function return type 'uint64_t' (aka 'unsigned long long') uint64_t primary_key() const { return uuid;}

0

You can't use a string as index. The documentation says:

They are multi index tables because they support using multiple indexes on the data, the primary index type must be uint64_t and must be unique, but the other, secondary, indexes can have duplicates. You can have up to 16 additional indexes and the field types can be uint64_t, uint128_t, uint256_t, double or long double

So you probably have to do something like this:

struct [[eosio::table]] blackboxdata {
    name         user;
    uint64_t     id;
    uint128_t    uuid;
    std::string  hash;
    std::string  sqlrequest;
    uint64_t     primary_key() const { return id;}
    uint128_t    secondary_key() const { return uuid;}
};

take a look here on how to convert a uuid to a uint128_t.

If each user is unique,you could use it instead of the additonal uint64_t as primary index.

| improve this answer | |
0

Thanks for your answer,

1- I followed your instruction above but I have 2 errors I put them in comment

2- I have a "hashcompare" action but I don't know how I can compare the hash coming from the data base with the hash stored in the blockchain using uuid with this action.

This my full code

#include <eosio/eosio.hpp>

using namespace eosio;

class [[eosio::contract("blackbox")]] blackbox : public eosio::contract {
  public:
      blackbox(name receiver, name code,  datastream<const char*> ds): contract(receiver, code, ds) {}

    [[eosio::action]]
    void add(const name user, uint64_t id, std::string uuid,  std::string hash, std::string sqlrequest, std::string date){
        require_auth( user );

        blackboxdata_index addresses( get_self(), get_first_receiver().value );

      std::replace( uuid.begin(), uuid.end(),'-',''); //15:50 error: expected expression
      uuid = "Ox"+uuid; 
      uint128_t indexable_uuid = std::strtoul(uuid, 0, 16); //17:47: error: no viable conversion from 'std::string' (aka 'basic_string<char,char_traits<char>, allocator<char> >') to 'const char *'

        addresses.emplace(user, [&]( auto& row ) {
            row.user = user;
        row.id = id;
            row.uuid = indexable_uuid;
            row.hash = hash;
            row.sqlrequest =sqlrequest;
            row.date = date;
        });
    } 

    [[eosio::action]]
    void hashcompare (name user, std::string uuid_database, std::string hash_database){
       require_auth (user)
       blackboxdata_index addresses( get_self(), get_first_receiver().value );

       //how I can compare the hash coming from the data base with the hash stored in the blockchain using uuid with this action.
    }


  private: 
    struct [[eosio::table]] blackboxdata {
    name         user;
    uint64_t     id;
    uint128_t    uuid;
    std::string  hash;
    std::string  sqlrequest;
    std::string  date;
    uint64_t     primary_key() const { return id;}
    uint128_t    secondary_key() const { return uuid;}
};
  typedef eosio::multi_index<"data"_n, blackboxdata,
  indexed_by<"hash"_n, const_mem_fun<blackboxdata, uint128_t, &blackboxdata::secondary_key> > >blackboxdata_index;

};
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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