多索引表 (8)表操作
生活随笔
收集整理的這篇文章主要介紹了
多索引表 (8)表操作
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. EOSIO Multi-Index API使用Multi-Index表
提供了C++接口,它的原型實(shí)際是Boost Multi-index Containers;
Multi-Index DB是一種在RAM中緩存狀態(tài)或數(shù)據(jù)以便快速訪(fǎng)問(wèn)的方法。它支持創(chuàng)建,讀取,更新和刪除操作,特點(diǎn)就和它的名字一樣——多索引。
- 增加 emplace
- 修改 modify
- 刪除 erase
- 查找包括find和get,以及迭代器操作
1.1 find
const_iterator eosio::multi_index< TableName, T, Indices >::find(uint64_t primary ) constconst_iterator eosio::multi_index< TableName, T, Indices >::require_find(uint64_t primary,const char * error_msg = "unable to find key" ) const1.2 delete
void eosio::multi_index< TableName, T, Indices >::erase(const T & obj )eg.
void myaction() {auto itr = addresses.find("dan"_n);eosio::check(itr != addresses.end(), "Record is not found");addresses.erase(*itr);itr = addresses.find("dan"_n);eosio::check(itr == addresses.end(), "Record is not deleted"); }void myaction() {// create reference to address_index - see emplace example// add dan account to table - see emplace exampleauto itr = addresses.find("dan"_n);eosio::check(itr != addresses.end(), "Address for account not found");addresses.erase( itr );eosio::check(itr != addresses.end(), "Everting lock arf, Address not erased properly");}[[eosio::action]] void multi_index_example::del( name user ) {// check if the user already existsauto itr = testtab.find(user.value); + if ( itr == testtab.end() ) { + printf("user does not exist in table, nothing to delete" ); + return; + }+ testtab.erase( itr ); }1.3 insert
template<typename Lambda> const_iterator eosio::multi_index< TableName, T, Indices >::emplace(name payer,Lambda && constructor )eg.
[[eosio::action]] void multi_index_example::set( name user ) {// check if the user already existsauto itr = testtab.find(user.value);+ if ( itr == testtab.end() ) { + testtab.emplace( _self, [&]( auto& u ) { + u.test_primary = user; + u.secondary = "second"_n; + u.datum = 0; + }); + } }1.4 modify
template<typename Lambda> void eosio::multi_index< TableName, T, Indices >::modify(const_iterator itr,name payer,Lambda && updater )eg.
void myaction() {// create reference to address_index - see emplace example// add dan account to table - see emplace exampleauto itr = addresses.find("dan"_n);eosio::check(itr != addresses.end(), "Address for account not found");addresses.modify( *itr, payer, [&]( auto& address ) {address.city = "San Luis Obispo";address.state = "CA";});eosio::check(itr->city == "San Luis Obispo", "Lock arf, Address not modified");} }void myaction() {// create reference to address_index - see emplace example// add dan account to table - see emplace exampleauto itr = addresses.find("dan"_n);eosio::check(itr != addresses.end(), "Address for account not found");addresses.modify( itr, account payer, [&]( auto& address ) {address.city = "San Luis Obispo";address.state = "CA";});}[[eosio::action]] void multi_index_example::mod( name user, uint32_t value ) {// check if the user already existsauto itr = testtab.find(user.value);check( itr != testtab.end(), "user does not exist in table" );+ testtab.modify( itr, _self, [&]( auto& row ) { + row.secondary = user; + row.datum = value; + }); }總結(jié)
以上是生活随笔為你收集整理的多索引表 (8)表操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 多索引表 (5)创建多索引表
- 下一篇: EOS 智能合约源代码解读 (10)to