USAGE EOS CLEOS - EASY #3

in #eos6 years ago (edited)

HOW USE DATABASE ON CONTRACT

MULTY INDEX USAGE

CONTRACT SAMPLE

#include <utility>
#include <vector>
#include <string>
#include <eosiolib/crypto.h>
#include <eosiolib/types.hpp>
#include <eosiolib/print.hpp>
#include <eosiolib/action.hpp>
#include <eosiolib/multi_index.hpp>
#include <eosiolib/contract.hpp>

#include <eosio.system/eosio.system.hpp>
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

Sort:  

Hey bro!.. @riccotz
Pls post on Utopian community platform residing on Steem blockchain. It's for the coders.
U can earn more....I

I try to get table but failed:

$ cleos get table usagedbc usagedbc db_struct
Error 3120001: Invalid name
Name should be less than 13 characters and only contains the following symbol .12345abcdefghijklmnopqrstuvwxyz
Error Details:
Name not properly normalized (name: db_struct, normalized: db.struct) 

Hi, you not right use it
cleos get table usagedbc usagedbc db_struct

instead db_struct you need set table name, on it sample table name "dbtablename"

#right #usage
cleos get table usagedbc usagedbc dbtablename

Great post!

One question, how do you retrieve the table(s) according to a certain search criteria?

i.e. retrieve only the table with certain ids?

Hi! I think it not possible, you need know table name or - add table where store all table names

Hi @Riccots, thanks for your reply. I did not understand the second part of your answer. What do you mean by storing all table names? Can DAtabase API be in eos used for this? Thanks.

Nice Post dear. Looking forward to read your next post.

I have followed you, follow me back to be getting up votes from me on your future posts. https://steemit.com/@nwanne

Thank you.

Thanks you! i have add more better help posts.

i will try :)

Coin Marketplace

STEEM 0.28
TRX 0.12
JST 0.033
BTC 69386.82
ETH 3714.50
USDT 1.00
SBD 3.85