多索引表 (5)创建多索引表
生活随笔
收集整理的這篇文章主要介紹了
多索引表 (5)创建多索引表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 創建表
- class/struct 定義一個持久化對象。
- 該對象需要有一個const的成員作為主鍵,類型為uint64_t
- 二級主鍵可選,提供不同的鍵類型
- 為每個二級索引定義一個鍵導出器,鍵導出器是一個函數,可以用來從Multi_index表中獲取鍵
multi_index_example.hpp
#include <eosio/eosio.hpp> using namespace eosio;// multi index example contract class class [[eosio::contract]] multi_index_example : public contract {public:using contract::contract;// contract class constructormulti_index_example( name receiver, name code, datastream<const char*> ds ) :// contract base class contructorcontract(receiver, code, ds),// instantiate multi index instance as data member (find it defined below)testtab(receiver, receiver.value){ }// the row structure of the multi index table, that is, each row of the table// will contain an instance of this type of structurestruct [[eosio::table]] test_table {// this field stores a name for each row of the multi index tablename test_primary;// additional data stored in table rowuint64_t datum;// mandatory definition for primary key getteruint64_t primary_key( ) const { return test_primary.value; }};// the multi index type definition, for ease of use define a type alias `test_tables`, // based on the multi_index template type, parametarized with a random name and // the test_table data structuretypedef eosio::multi_index<"testtaba"_n, test_table> test_tables;// the multi index table instance declared as a data member of type test_tablestest_tables testtab;[[eosio::action]] void set( name user );[[eosio::action]] void print( name user );using set_action = action_wrapper<"set"_n, &multi_index_example::set>;using print_action = action_wrapper<"print"_n, &multi_index_example::print>; };總結
以上是生活随笔為你收集整理的多索引表 (5)创建多索引表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多索引表 (4)multi_index.
- 下一篇: 多索引表 (8)表操作