`

EOS智能合约-增删改查

    博客分类:
  • EOS
 
阅读更多
#include <eosiolib/eosio.hpp>

class [[eosio::contract]] testcontract : public eosio::contract
{
public:
    testcontract( eosio::name receiver, eosio::name code, eosio::datastream<const char*> ds )
        : eosio::contract(receiver, code, ds),  _students(receiver, code.value)
    {}

    //添加学生
    [[eosio::action]]
    void add(eosio::name user, std::string studentName,std::string studentGender,uint32_t studentAge,uint32_t studentHeight)
    {
        require_auth(user);
        eosio::print("Add student ", studentName);
        _students.emplace(get_self(), [&](auto& p) {
            p.id = _students.available_primary_key();
            p.studentName = studentName;
            p.studentGender = studentGender;
            p.studentAge = studentAge;
            p.studentHeight = studentHeight;
        });
    }

    //根据主键删除学生
    [[eosio::action]]
    void del(eosio::name user, uint64_t id)
    {
        require_auth(user);
        eosio::print("Del student", id);
        auto itr = _students.find(id);
        if (itr != _students.end()) {
            _students.erase(itr);
        }
    }

    //根据主键修改学生
    [[eosio::action]]
    void update(eosio::name user, uint64_t id,std::string studentName,std::string studentGender,uint32_t studentAge,uint32_t studentHeight)
    {
        require_auth(user);
        eosio::print("update student", studentName);
        auto itr = _students.find(id);
        if (itr != _students.end())
        {
            _students.modify(itr, user, [&]( auto& p ) {
                p.studentName = studentName;
                p.studentGender = studentGender;
                p.studentAge = studentAge;
                p.studentHeight = studentHeight;
            });
        }
    }

    //清空表,待定
    [[eosio::action]]
    void clear(eosio::name user)
    {
        require_auth(user);
        std::vector<uint64_t> keysForDeletion;
        for(auto& item : _students) {
            keysForDeletion.push_back(item.id);
        }

        for (uint64_t key : keysForDeletion) {
            eosio::print("remove from _students ", key);
            auto itr = _students.find(key);
            if (itr != _students.end()) {
                _students.erase(itr);
            }
        }
    }

    //private: -- not private so the cleos get table call can see the table data.
    // create the multi index tables to store the data
    struct [[eosio::table]] students
    {
        uint64_t      id; // primary key
        std::string   studentName;
        std::string   studentGender;
        uint32_t    studentAge;
        uint32_t    studentHeight;

        uint64_t primary_key() const { return id; }
        uint64_t by_age() const {return studentAge; }
    };

    //数据表根据age排序
    typedef eosio::multi_index<"students"_n, students, eosio::indexed_by<"studentage"_n, eosio::const_mem_fun<students, uint64_t, &students::by_age>>> studentstable;

    //students数据库表
    studentstable _students;
};

EOSIO_DISPATCH( testcontract, (add)(del)(update)(clear))



1.编译与部署合约
xjdeMacBook-Pro:testcontract xj$ eosio-cpp -o testcontract.wasm testcontract.cpp --abigen
Warning, empty ricardian clause file
Warning, empty ricardian clause file
Warning, action <add> does not have a ricardian contract
Warning, action <del> does not have a ricardian contract
Warning, action <update> does not have a ricardian contract
Warning, action <clear> does not have a ricardian contract
(reverse-i-search)`':
xjdeMacBook-Pro:testcontract xj$ cleos set contract testcontract /Users/xj/code/eos/build/programs/cleos/testcontract -p testcontract@active
Reading WASM from /Users/xj/code/eos/build/programs/cleos/testcontract/testcontract.wasm...
Skipping set code because the new code is the same as the existing code
Skipping set abi because the new abi is the same as the existing abi
no transaction is sent

2.添加数据
xjdeMacBook-Pro:testcontract xj$ cleos push action testcontract add '["testcontract","wangwu","male",11,120]' -p eosio@active
executed transaction: 4ea9886d0347e75028ef5f4d2180ceee2f5a8843783f30ee7fb3a473b3dc2039  120 bytes  1308 us
#  testcontract <= testcontract::add            {"s":"testcontract","studentName":"wangwu","studentGender":"male","studentAge":11,"studentHeight":12...
warning: transaction executed locally, but may not be confirmed by the network yet         ]

3.查询
xjdeMacBook-Pro:testcontract xj$ cleos get table testcontract testcontract students
{
  "rows": [{
      "id": 0,
      "studentName": "wangwu",
      "studentGender": "male",
      "studentAge": 11,
      "studentHeight": 120
    }
  ],
  "more": false
}

4.修改
xjdeMacBook-Pro:testcontract xj$ cleos push action testcontract update '["testcontract",0,"wangwu","male",12,123]' -p eosio@active
executed transaction: b895a437c9c205d769cdb3746b861b5164591f4cec70bfb047d5e0472e21a18b  128 bytes  1198 us
#  testcontract <= testcontract::update         {"s":"testcontract","id":0,"studentName":"wangwu","studentGender":"male","studentAge":12,"studentHei...
warning: transaction executed locally, but may not be confirmed by the network yet         ]
xjdeMacBook-Pro:testcontract xj$ cleos get table testcontract testcontract students
{
  "rows": [{
      "id": 0,
      "studentName": "wangwu",
      "studentGender": "male",
      "studentAge": 12,
      "studentHeight": 123
    }
  ],
  "more": false
}

5.删除
xjdeMacBook-Pro:testcontract xj$ cleos push action testcontract del '["testcontract",0]' -p eosio@active
executed transaction: f885c9e315a2d610fbeeefc887884c1e9b99ed21c1782bc1034eb67c6e3fda96  112 bytes  1172 us
#  testcontract <= testcontract::del            {"s":"testcontract","id":0}
warning: transaction executed locally, but may not be confirmed by the network yet         ]
xjdeMacBook-Pro:testcontract xj$ cleos get table testcontract testcontract students
{
  "rows": [],
  "more": false
}
分享到:
评论

相关推荐

    《玩转EOS智能合约.doc》详细介绍了对EOS智能合约从部署到使用。

    1、一般来讲,对数据库的操作无外乎增删改查 2、表结构示例详解 3、Multi_index定义,建立数据表 4、实例化multi_index 5、操作数据,增删改查 #玩转table表 1、Table表producers 2、Table表global 3、...

    EOS智能合约开发详细教程

    EOS智能合约开发是一项涉及到区块链技术、合约编写和执行的技术,它允许开发者在EOS平台上创建和部署智能合约,为去中心化应用(DAPP)提供运行的基础。本文将详细探讨EOS智能合约开发的相关知识点。 首先,EOS智能...

    EOS智能合约教程,从零开始学习EOS智能合约

    EOS智能合约教程,从零开始学习EOS智能合约, DApp安全漏洞(security)及攻击实践(EOS Contract)

    EOS智能合约开发教程

    EOS智能合约详细开发教程。这是本人整理自币乎-松果的文章,这可能是当前最详细的EOS开发教程了吧,花了我一整天的时间进行整理,很全面,分享给有需要的朋友。

    eos-crypto-java_ecc_

    `eos-crypto-java_ecc_`这个项目显然关注的是在Java平台上实现ECC加密算法,特别是针对EOS区块链平台的加密需求。 在Java中,ECC的实现主要依赖于Java Cryptography Extension (JCE)框架,它是Java标准库的一部分,...

    佳能单反SDK最新版 Canon_EOS_ED-SDK_v3.6.10

    EOS-1D Mark III EOS 40D EOS-1Ds Mark III EOS DIGITAL REBEL Xsi/450D/ Kiss X2 EOS DIGITAL REBEL XS/ 1000D/ KISS F EOS 50D EOS 5D Mark II EOS Kiss X3/EOS REBEL T1i /EOS 500D EOS 7D EOS-1D Mark ...

    cpp-eos3dio和eosdayioEOS智能合约

    【标题】"cpp-eos3dio和eosdayioEOS智能合约"涉及的是使用C++语言在EOS区块链平台上开发智能合约的技术。EOS是一个基于区块链技术的分布式应用操作系统,它旨在提供一个去中心化、高性能的环境来支持大规模的商业级...

    eos-tutorials-源码.rar

    本教程将通过深入解析"eos-tutorials-源码"来帮助你理解EOS的工作原理及其开发流程。 1. **EOS项目结构** EOS源码通常按照模块进行组织,包括共识机制、网络通信、智能合约、账户系统等。在"eos-tutorials-源码"中...

    eos4j-1.0.2.jar

    这是eos api开源项目eos4j-1.0.2.jar,下载完请解压,源码地址:https://github.com/espritblock/eos4j

    PyPI 官网下载 | eos-py-1.0.1.tar.gz

    通过“eos-py”,Python开发者能够轻松地与EOS网络进行交互,实现智能合约的部署、调用等操作。 首先,让我们解压“eos-py-1.0.1.tar.gz”这个文件。在Unix/Linux系统中,可以使用`tar -zxvf eos-py-1.0.1.tar.gz`...

    PyPI 官网下载 | eos-py-0.18.0.tar.gz

    因此,`eos-py`很可能是为开发者提供与EOS区块链交互的Python接口,包括账户管理、交易发送、智能合约部署等功能。 在实际使用中,开发者首先需要将`.tar.gz`文件解压缩,然后通过Python的`setup.py`脚本来安装这个...

    普元EOS-Platform-7.0基础开发教程完整版

    ### 普元EOS-Platform-7.0基础开发教程知识点详析 #### 一、产品概述 **1.1 产品简介** 普元EOS Platform 7.0是一款基于J2EE、Eclipse等开放技术和平台的产品,它通过配置化、组件化、图形化和一体化的方式,为...

    Python库 | eos_py-0.14.0-cp36-cp36m-win_amd64.whl

    python库,解压后可用。 资源全名:eos_py-0.14.0-cp36-cp36m-win_amd64.whl

    EOS示例与教程.chm

    01.01 快速开发:单表增删改查 01.01.01 页面效果 01.01.02 数据准备 01.01.03 创建数据模型 01.01.04 设置显示属性 01.01.05 增删改查一步完成 01.01.05.01 设置查询条件 01.01.05.02 选择目录并通过向导...

    Python-EOS智能合约与DApp开发入门实战教程

    EOS智能合约与DApp开发入门实战教程:课程帮助你快速入门EOS区块链去中心化应用的开发,内容涵盖EOS工具链、账户与钱包、智能合约开发与部署、代币发行、使用代码与合约交互等核心知识点,并结合React完成一个便签...

    eos-new-table:生成EOS C ++智能合约以在区块链上创建表

    生成EOS C ++智能合约以在区块链上创建表。 安装 通过以下方式全局安装: npm i -g eos-new-table 用法 选项 $ eos-new-table Generate EOS C++ smart contract to create a table on the blockchain. Options: -...

    学习EOS的资料--EOS故障处理指南

    EOS Studio是一款EOS开发工具,它为开发者提供了图形化的界面来编写、部署和管理智能合约。其故障处理可能涉及界面显示、代码编译、合约部署等环节,需要检查工具配置、更新版本、修复网络连接或优化合约代码。 ...

    EOS发布WebService-服务端

    标题中的“EOS发布WebService-服务端”指的是EOS(Enterprise Object Services)系统开发中关于构建Web服务服务器端的部分。EOS是一个面向对象的服务框架,通常用于构建分布式应用和企业级系统。在这样的系统中,...

Global site tag (gtag.js) - Google Analytics