logo
Inery

7 months ago

IneryDB: How to Insert, Modify, and Remove Data

article_image

See more news

news_image
The World Wide Web Made For Users - By Users
news_image
Building Trust in Digital Transactions: The Role of Decentralized Identity Systems

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

1 year ago

Inery & Power Companies – Power of Data

The applications of Inery in the power sector ...READ MORE

artilce_image

Share

logo
Inery

1 year ago

Inery & Land and Real-Estate Authorities – A Common Ground

Diving deep into Inery's core strengths to offer opportunities and combat issues in the Land and Real-Estate department ...READ MORE

artilce_image

Share

logo
Inery

1 year ago

Advisor Spotlight — Bally Singh

A web 3.0 marketing wizard and an ingenious entrepreneur join as an advisor of Inery. ...READ MORE

artilce_image

Share

logo
Inery

4 months ago

IneryDB 101 - Everything I Need To Know

Whether you're a beginner or tech enthusiast, IneryDB awaits—unlock its full potential and stay informed about upcoming features for a seamless experience! ...READ MORE

artilce_image

Share

bgbg