
USAGE EOS CLEOS - EASY #3
HOW USE DATABASE ON CONTRACT
MULTY INDEX USAGE
CONTRACT SAMPLE
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace eosio;
using eosio::indexed_by;
using eosio::const_mem_fun;
using eosio::asset;
class usagedbc : public eosio::contract {
public:
usagedbc( account_name self )
:contract(self),
dbusage(_self, _self)
{}
//@abi action
void actionget( const uint64_t unique_id, const uint64_t delrow ) {
std::string valv1 = "testval1";
std::string valv2 = "testval2";
auto dbusage_itr = dbusage.find(unique_id);
if(dbusage_itr == dbusage.end()){
print("TRY Row not found\n");
// Store new data
auto new_dbusage_itr = dbusage.emplace(_self, [&](auto& db_struct){
db_struct.id = unique_id;//dbusage.available_primary_key();
db_struct.val1 = valv1;
db_struct.val2 = valv2;
db_struct.date_create = now() + FIVE_MINUTES;
});
dbusage_itr = dbusage.find(unique_id);
if(dbusage_itr == dbusage.end()){
print("GET Row not found\n");
}
if(dbusage_itr->id == unique_id){
print("GET Row founded\n");
if(delrow == 1){
dbusage.erase(dbusage_itr);
}
}
}
else if(dbusage_itr->id == unique_id){
print("TRY Row founded\n");
if(delrow == 1){
dbusage.erase(dbusage_itr);
}
}
}
private:
//@abi table dbtablename i64
struct db_struct {
uint64_t id;
std::string val1;
std::string val2;
time date_create;
uint64_t primary_key()const { return id; }
EOSLIB_SERIALIZE( db_struct, (id)(val1)(val2)(date_create) )
};
typedef eosio::multi_index< N(dbtablename), db_struct> db_index;
db_index dbusage;
};
EOSIO_ABI( usagedbc, (actionget) )
WE GENERATE WAST
eosiocpp -o /root/eos/contracts/usagedbc/usagedbc.wast /root/eos/contracts/usagedbc/usagedbc.cpp
WE GENERATE ABI
eosiocpp -g /root/eos/contracts/usagedbc/usagedbc.abi /root/eos/contracts/usagedbc/usagedbc.cpp
WE PUBLISH CONTRACT
cleos set contract myaccount /root/eos/contracts/usagedbc/ -p myaccount
USAGE THIS CONTRACT
#ARG unique_id -> unique id on database, delrow -> if 0 - not deleted row, if 1 - founded row deleted
cleos/cleos push action myaccount actionget '{"unique_id":100, "delrow":0}' -p myaccount --json
READ TABLE
cleos get table myaccount myaccount dbtablename
Leave USAGE EOS CLEOS - EASY #3 to:
Read more #eos posts
Best Posts From riccotz
We have not curated any of riccotz's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.