logo
Inery

10 months ago

IneryDB: How to Insert, Modify, and Remove Data

article_image

See more news

news_image
How Decentralization Empowers Digital Freedom: The Data Rebellion
news_image
Unbundling Digital Identities: Adopting the New Way in Metaverse

Getting basic table operations done in IneryDB is fairly simple. In this guide, we embark on a journey through the intricate landscape of data manipulation, focusing on the fundamental operations of data insertion, modification, and removal, especially within the context of multi-index tables.


How to Insert Data


Inserting data in IneryDB can be broken down into two steps: checking whether the user data is already in the database and inserting said data if it isn’t.


1. Verify If The User Already Exists


First, we need to see if the user object already exists. We’ll do this with the multi-index table iterator.


We can search the user based on the account name like this:


[[inery::action]] void multi_index_example::set( name user ) {


 // check if the user already exists


 auto itr = datatable.find(user.value);


}


2. Insert The User If Not Found In Table


In case the user isn’t already in the multi-index table, we may insert their data. You can use the emplace method to make the insertion. Otherwise, print an informational message.


[[inery::action]] void multi_index_example::set( name user ) {


 // check if the user already exists


 auto itr = datatable.find(user.value);


+  if ( itr == datatable.end() ) {


+    datatable.emplace( _self, [&]( auto& u ) {


+      u.test_primary = user;


+      u.secondary = "second"_n;


+      u.datum = 0;


+    });


+  }


+  else {


+    printf("User already exists.");


+  }


}


How to Modify Data


Here is a three-step rundown of how to modify data in the datatable multi-index table.


1. Define The mod( ) Action


First, you need to add a mod action to the datatable multi-index table. 


Add a mod action to the datatable multi-index table. The mod action takes a user of type inery::name and a value of type uint32_t as input parameters. The mod action also updates the user object datum data member with the uint32_t value.


[[inery::action]] void mod( name user, uint32_t value );


For ease of use, consider adding the action wrapper definition, too (this is optional).


[[inery::action]] void mod( name user, uint32_t value );


+using mod_action = action_wrapper<"mod"_n, &multi_index_example::mod>;


2. Find The User You Want To Modify


Next, we need to find the user whose data needs modifying. Let’s use the multi-index find( ) method to locate the user object. Again, the targeted user is searched based on the account name.


[[inery::action]] void multi_index_example::mod( name user, uint32_t value ) {


 auto itr = datatable.find(user.value);


}


Submit Error If User Not Found


If the user object you’re trying to update is not found using the above method, try raising an error message via the inery::check method:


[[inery::action]] void multi_index_example::mod( name user, uint32_t value ) {


auto itr = datatable.find(user.value);


+  check( itr != datatable.end(), "user does not exist in table" );


}


In case the user object you want to update is found, inery::check will do nothing. Meanwhile, the itr iterator will be pointing at the object you want to update. 


3. Update The User


From here, we can update the user object. Use the multi-index::modify( ) method to update the user object datum data member with the value parameter.


[[inery::action]] void multi_index_example::mod( name user, uint32_t value ) {


 // check if the user already exists


auto itr = datatable.find(user.value);


check( itr != datatable.end(), "user does not exist in table" );


+  datatable.modify( itr, _self, [&]( auto& row ) {


+    row.datum = value;


+  });


}


You have now implemented a new action mod. Call mod to update the datum data member for the user object identified by the user name parameter.


How to Remove Data


Once again, deleting data in IneryDB is possible with two quick steps. Do the following to implement a del action that deletes a user object (identified by its account name) from the multi-index table.


1. Find The User You Want To Delete


Locate the user object you want to delete through the multi-index find( ) method. The targeted user is searched via its account name.


[[inery::action]] void multi_index_example::del( name user ) {


 // check if the user already exists


 auto itr = datatable.find(user.value);


}


2. Delete The User If Found


Upon finding the user in question, use erase() method to delete the row from the table. Otherwise, print an informational message and return.


[[inery::action]] void multi_index_example::del( name user ) {


 // check if the user already exists


auto itr = datatable.find(user.value);


+  if ( itr == datatable.end() ) {


+    printf("User does not exist in table, nothing to delete");


+    return;


+  }


+  datatable.erase( itr );


}


Final words


Congratulations! You've just taken a deep dive into the world of IneryDB, uncovering the art of data manipulation. From seamlessly inserting and modifying data to orchestrating graceful removals, you've discovered the building blocks that craft a data-rich symphony. But this is only the beginning.


At Inery, we believe that data isn't just information; it's a catalyst for innovation. It's the raw material that fuels your ideas, your projects, and your dreams. That's why IneryDB isn't just another platform; it's your creative canvas for sculpting solutions, generating insights, and pioneering change.


Ready to unleash the true potential of your data? Explore IneryDB's limitless possibilities, where each action you've learned today is just a glimpse of what's in store. Join us on a journey that empowers you to shape your data destiny. Click here to embark on an adventure that transforms data into a force of nature, and you into a data virtuoso. Welcome to the future of data management. Welcome to IneryDB.

logo
Inery

6 months ago

Fighting Algorithmic Bias – Ethical Dilemmas and AI

AI's rapid evolution raises ethical concerns, emphasizing the need for fair and transparent applications through diverse datasets, audits, and societal changes for an inclusive and ethical future. ...READ MORE

artilce_image

Share

logo
Inery

2 years ago

Inery Discord Invitation Challenge

Get a chance to win guaranteed allocation in Inery´s launch event and cashback! ...READ MORE

artilce_image

Share

logo
Inery

1 year ago

The Top 5 Layer-1 Blockchains: Building the Foundation

These top layer-1 blockchain projects are setting the standard for cryptos and decentralized technology. ...READ MORE

artilce_image

Share

logo
Inery

1 year ago

Inery At Next Block Expo

Inery's team joined founders, investors, blockchain experts, and crypto enthusiasts, from all over the world to discuss and redefine the future of Web3. ...READ MORE

artilce_image

Share

bgbg