The basic operation of storing and handling information in tables is typically known as “CRUD”, which stands for “Create,” “Read,” “Update,” and “Delete.” In the previous post, we went over the “Create” part, so we will cover the “Read,” “Update,” and “Delete” parts in this post. In general, you would need to configure separate tables for each function, but we will just use the ttab table from the previous example so that we can easily handle the contents with minimal configuration.

auto iter=ttabs.find(user);

if(iter==ttabs.end())

{

print("need insert\t");

ttabs.emplace(_self,[&](auto& ttab)

{

ttab.to = user;

});

}

else

{

uint64_t getuser;

getuser=iter->to;

print("data already exist ",name{getuser}," \t");

}

A code has been added to the else in the action hi part from the previous example. Search for the username passed onto action hi in the first line of the code above, and if there is no data retrieved (meaning the above code comes out as true), make new data. If there is data retrieved, bring the to field of that data line. (The two lines above the else statement). Other than this, there is also get() function as persistence api, but it’d be better to learn about these by expanding on them one by one after you understand the overall flow with reference to this post.

If you compile and upload the contract, then execute as below,

#cleos push action usersc hi ‘[“testuser”]’ -p usersc

since the testuser is in the table as above, you get the testuser that exists in the table, not the data that passed in the action data, to print.

To delete data, add an action.

void del(account_name user)

{ }

The method is the same as adding an action from the previous example. Because we need the username that needs to be deleted, get an account name as a parameter. For convenience sake, we will not check if the username received from the action data is valid.

void del(account_name user)

{

_ttab ttabs(_self,_self);

auto iter=ttabs.find(user);

if(iter!=ttabs.end())

{

ttabs.erase(iter);

}

}

Implement the delete action.

Similar to what we did with create, for the first and second lines create a multi_index instance of the table, find the received user information, and assign it to the iterator.

If the result of find in the third line “if(iter!=ttabs.end())” is not the last, meaning there is data passed through action,

delete the data with “ttabs.erase(iter)”. Use the erase() function of the multi_index instance to delete the information in the table by passing the iterator to be deleted.

If you look at the table before compiling and running the contract,

# cleos get table usersc usersc ttab

you can see that testuser exists in the table.

# cleos push action usersc del ‘[“testuser”]’ -p usersc

Try running the contract and deleting testuser.

Because no print statements for debugging was placed in action, it ends without any printing.

Modification of data requires the modification of table. This is because the currently configured table is composed of only one field, and that one field is composed of a primary key and therefore cannot be modified.



struct edittab

{

account_name to;

string tstr;

uint64_t primary_key() const {return to;} /// @abi table edittab i64struct edittabaccount_name to;string tstr;uint64_t primary_key() const {return to;} EOSLIB_SERIALIZE(edittab,(to)(tstr))

}; typedef multi_index<N(edittab),edittab> _etab;

Create the new table information under the table information you created above. The new table is a table named edittab that has the string information tstr with the account information to as the primary key. Since the table we will create will not have any information in it, modify the hi action, which can create data row.

void hi(account_name user)

{

_etab etabs(_self,_self);



auto iter=etabs.find(user);

if(iter==etabs.end())

{

print("need insert\t");

etabs.emplace(_self,[&](auto& edittab)

{

edittab.to = user;

edittab.tstr = "test";

});

}

else

{

uint64_t getuser;

getuser=iter->to;

print("data already exist ",name{getuser}," \t");

}

print("hello, world : ", name{user});

}

Only the table’s reference variable name of the previous example has changed, and if there is no account information received as action data in the table, save the “test” string in the account information and tstr sections.

void edit(account_name org)

{

_etab etabs(_self,_self);

auto iter=etabs.find(org);

if(iter!=etabs.end())

{

etabs.modify(iter,_self,[&](auto& a)

{

a.tstr="tested";

});

}

}

Now add the edit action. Up to the retrieval of the given account information on the edit action is the same as the other examples above. If a same account is found, use the modify function to modify the string “test” to “tested”.

Next, compile and upload the contract.

Since the current table’s edittab will not have any information,

# cleos push action usersc hi ‘[“a”]’ -p usersc

Use the hi action to insert a table.

# cleos get table usersc usersc edittab

Use the command above to check the table.

Notice that the “a” user contains the string “test”.

# cleos push action usersc edit ‘[“a”]’ -p usersc

Call the edit action to modify.

# cleos get table usersc usersc edittab

Use the above command to check the table.

You can see that the information in the table has changed.

This makes it possible to handle information via CRUD within EOS. Although this was a simple example, if you look at the official document’s other examples now, it’ll be much easier to understand than before.

Although there was a slight problem with the table design when modifying, and even though the example wasn’t completely clear in showing how one contract can be used to use multiple tables, we still proceeded with it.

Below is the code we used in this post.

#include <eosiolib/eosio.hpp>

#include <eosiolib/print.hpp> using namespace eosio;

using namespace std; class hello : public eosio::contract

{

public:

using contract::contract;

struct ttab

{

account_name to;

uint64_t primary_key() const {return to;} /// @abi table ttab i64struct ttabaccount_name to;uint64_t primary_key() const {return to;} EOSLIB_SERIALIZE(ttab,(to))

}; typedef multi_index<N(ttab),ttab> _ttab;

struct edittab

{

account_name to;

string tstr;

uint64_t primary_key() const {return to;} /// @abi table edittab i64struct edittabaccount_name to;string tstr;uint64_t primary_key() const {return to;} EOSLIB_SERIALIZE(edittab,(to)(tstr))

}; typedef multi_index<N(edittab),edittab> _etab;

void hi(account_name user)

{

_etab etabs(_self,_self);



auto iter=etabs.find(user);

if(iter==etabs.end())

{

print("need insert\t");

etabs.emplace(_self,[&](auto& edittab)

{

edittab.to = user;

edittab.tstr = "test";

});

}

else

{

uint64_t getuser;

getuser=iter->to;

print("data already exist ",name{getuser}," \t");

}

print("hello, world : ", name{user}, " 10");

} /// @abi actionvoid hi(account_name user)_etab etabs(_self,_self);auto iter=etabs.find(user);if(iter==etabs.end())print("need insert\t");etabs.emplace(_self,[&](auto& edittab)edittab.to = user;edittab.tstr = "test";});elseuint64_t getuser;getuser=iter->to;print("data already exist ",name{getuser}," \t");print("hello, world : ", name{user}, " 10"); void del(account_name user)

{

_etab etabs(_self,_self);

auto iter=etabs.find(user);

if(iter!=etabs.end())

{

etabs.erase(iter);

}

} void edit(account_name org)

{

_etab etabs(_self,_self);

auto iter=etabs.find(org);

if(iter!=etabs.end())

{

etabs.modify(iter,_self,[&](auto& a)

{

a.tstr="tested";

});

}

}

}; EOSIO_ABI(hello,(hi)(del)(edit))

In continuance of the last post, we went over how to use smart contract’s table information.