1. 先安装webase-front,使用它的IDE编译智能合约(可以查看我其它文章https://blog.csdn.net/u013288190/article/details/108762775)

2. 例子功能说明:https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/articles/3_features/35_contract/entry_quick_guide.html

3. 源码

StudentScoreByCRUD.sol

pragma solidity ^0.4.25;// import "./Table.sol";contract StudentScoreByCRUD{address private _owner;modifier onlyOwner{require(_owner == msg.sender, "Auth: only owner is authorized");_;}constructor () public {_owner = msg.sender;}event createEvent(address owner, string tableName);event insertEvent(address studentId, string courseName, int score);event updateEvent(address studentId, string courseName, int score);event removeEvent(address studentId, string courseName);// 创建成绩表function create() public onlyOwner returns(int){TableFactory tf = TableFactory(0x1001);int count = tf.createTable("stu_score", "student_id", "course_name, score");emit createEvent(msg.sender, "stu_score");return count;}// 插入成绩操作function insert(address studentId, string courseName, int score) public onlyOwner returns(int){TableFactory tf = TableFactory(0x1001);Table table = tf.openTable("stu_score");string memory stuIdStr = addressToString(studentId);Entry entry = table.newEntry();entry.set("student_id", stuIdStr);entry.set("course_name", courseName);entry.set("score", score);int count = table.insert(stuIdStr, entry);emit insertEvent(studentId, courseName, score);return count;}function addressToString(address addr) private pure returns(string) {// Convert addr to bytesbytes20 value = bytes20(uint160(addr));bytes memory strBytes = new bytes(42);// Encode hex prefixstrBytes[0] = '0';strBytes[1] = 'x';// Encode bytes usig hex encodingfor(uint i = 0; i < 20; i++){uint8 byteValue = uint8(value[i]);strBytes[2 + (i<<1)] = encode(byteValue >> 4);strBytes[3 + (i<<1)] = encode(byteValue & 0x0f);}return string(strBytes);}function encode(uint8 num) private pure returns(byte){// 0-9 -> 0-9if(num >= 0 && num <= 9){return byte(num + 48);}// 10-15 -> a-freturn byte(num + 87);}// 更新成绩操作function update(address studentId, string courseName, int newScore) public onlyOwner returns(int){TableFactory tf = TableFactory(0x1001);Table table = tf.openTable("stu_score");Entry entry = table.newEntry();entry.set("score", newScore);string memory stuIdStr = addressToString(studentId);Condition condition = table.newCondition();condition.EQ("student_id", stuIdStr);condition.EQ("course_name", courseName);int count = table.update(stuIdStr, entry, condition);emit updateEvent(studentId, courseName, newScore);return count;}// 删除成绩操作function remove(address studentId, string courseName) public onlyOwner returns(int){TableFactory tf = TableFactory(0x1001);Table table = tf.openTable("stu_score");string memory stuIdStr = addressToString(studentId);Condition condition = table.newCondition();condition.EQ("student_id", stuIdStr);condition.EQ("course_name", courseName);int count = table.remove(stuIdStr, condition);emit removeEvent(studentId, courseName);return count;}// 查询成绩操作function select(address studentId, string courseName) public view returns(int, string){TableFactory tf = TableFactory(0x1001);Table table = tf.openTable("stu_score");string memory stuIdStr = addressToString(studentId);Condition condition = table.newCondition();condition.EQ("student_id", stuIdStr);condition.EQ("course_name", courseName);Entries entries = table.select(stuIdStr, condition);if(entries.size() == 0){return (0, stuIdStr);}else{return (entries.get(0).getInt("score"), stuIdStr);}}}// table.sol,因为在webase-front的IDE中无法直接引用,所以将源码放到代码文件中contract TableFactory {function openTable(string memory) public view returns (Table) {} //open tablefunction createTable(string memory, string memory, string memory) public returns (int256) {} //create table}//select conditioncontract Condition {function EQ(string memory, int256) public {}function EQ(string memory, string memory) public {}function NE(string memory, int256) public {}function NE(string memory, string memory) public {}function GT(string memory, int256) public {}function GE(string memory, int256) public {}function LT(string memory, int256) public {}function LE(string memory, int256) public {}function limit(int256) public {}function limit(int256, int256) public {}}//one recordcontract Entry {function getInt(string memory) public view returns (int256) {}function getUInt(string memory) public view returns (int256) {}function getAddress(string memory) public view returns (address) {}function getBytes64(string memory) public view returns (bytes1[64] memory) {}function getBytes32(string memory) public view returns (bytes32) {}function getString(string memory) public view returns (string memory) {}function set(string memory, int256) public {}function set(string memory, uint256) public {}function set(string memory, string memory) public {}function set(string memory, address) public {}}//record setscontract Entries {function get(int256) public view returns (Entry) {}function size() public view returns (int256) {}}//Table main contractcontract Table {function select(string memory, Condition) public view returns (Entries) {}function insert(string memory, Entry) public returns (int256) {}function update(string memory, Entry, Condition) public returns (int256) {}function remove(string memory, Condition) public returns (int256) {}function newEntry() public view returns (Entry) {}function newCondition() public view returns (Condition) {}}contract KVTableFactory {function openTable(string memory) public view returns (KVTable) {}function createTable(string memory, string memory, string memory) public returns (int256) {}}//KVTable per permiary key has only one Entrycontract KVTable {function get(string memory) public view returns (bool, Entry) {}function set(string memory, Entry) public returns (int256) {}function newEntry() public view returns (Entry) {}}

4. 结果

FISCO BCOS Solidity 使用Table合约CRUD接口 智能合约例子相关推荐

  1. Solidity 官方文档中文版 2_Ethereum 智能合约介绍

    一个简单的智能合约 先从一个非常基础的例子开始,不用担心你现在还一点都不了解,我们将逐步了解到更多的细节. Storage contract SimpleStorage {uint storedDat ...

  2. FISCO BCOS区块链 修改增加RPC接口

    一.RPC RPC(Remote Procedure Call,远程过程调用)是客户端与区块链系统交互的一套协议和接口.用户通过RPC接口可查询区块链相关信息(如块高.区块.节点连接等)和发送交易. ...

  3. solidity开发以太坊代币智能合约

    智能合约开发是以太坊编程的核心之一,而代币是区块链应用的关键环节,下面我们来用solidity语言开发一个代币合约的实例,希望对大家有帮助. 以太坊的应用被称为去中心化应用(DApp),DApp的开发 ...

  4. c++ eos智能合约开发_EOS智能合约开发点滴记录-第二篇智能合约编写

    开发合约前,我们先选择下将要用的编辑工具 我常用的有 clion 和vscode,电脑os为mac,不过其他系统差别不大,如果你习惯用于Windows,那建议选择 Windows Subsystem ...

  5. fabric shim安装合约_智能合约简介_智能合约开发_Hyperledger Fabric_开发指南_区块链服务 BaaS - 阿里云...

    概述 在 Hyperledger Fabric 中,链码(Chaincode)又称为智能合约(下文中我们统一称为链码),是用Go,node.js或Java编写的程序,主要用于操作账本上的数据.用户的应 ...

  6. c++ eos智能合约开发_EOS智能合约开发为何编译成WebAssembly?

    许多人正试图学习如何在EOS上开发智能合约.但是,这些智能合约是由C++编写的,并编译成WebAssembly,这对大多数非c++程序员来说似乎很奇怪.因此,在深入了解EOS之前,最好先学习一些关于W ...

  7. FISCO BCOS 2.0发布:新增群组架构克服吞吐瓶颈

    今日,FISCO BCOS开源社区正式对外发布FISCO BCOS的2.0版,该版本在可扩展性.性能.易用性.隐私隔离等方面均取得突破性进展,其新增的群组架构方案,可以让企业间像拉微信群一样快速组链, ...

  8. FISCO BCOS 2.0 发布:新增群组架构克服吞吐瓶颈

    开发四年只会写业务代码,分布式高并发都不会还做程序员? >>>   3月20日,FISCO BCOS开源社区正式对外发布FISCO BCOS的2.0版,该版本在可扩展性.性能.易用性 ...

  9. 【“码”上有你】智能合约库有奖征码第3期来袭

    "智能合约库有奖征码"活动开展以来,伙伴们群策群力踊跃贡献,帮助夯实了合约库的基础功能,涌现了诸如共享经济.商品溯源等更多面向实际业务场景的合约样板,使得合约库更加满足开发者和行业 ...

  10. 智能合约编写之Solidity的设计模式 | FISCO BCOS超话区块链专场(篇4)

    前  言 随着区块链技术发展,越来越多的企业与个人开始将区块链与自身业务相结合. 区块链所具有的独特优势,例如,数据公开透明.不可篡改,可以为业务带来便利.但与此同时,也存在一些隐患.数据的公开透明, ...

最新文章

  1. Educational Codeforces Round 106 (Rated for Div. 2) D. The Number of Pairs 数论gcd
  2. php 一句话木马、后门
  3. PTA 7-55 剿灭魔教 (30分)(拓扑排序bfs版)
  4. 张志华 统计机器学习
  5. 从坚果3的发布来看,锤子未来的发展将依然艰难
  6. 正则表达式JS-1212
  7. Mybatisplus argument type mismatch
  8. 微视linux scsi驱动超时错误处理
  9. Qt FFmpeg视频播放器开发(八):播放器UI改造、高仿QQ影音
  10. matlab 列维 第一维行维 第二维,基于综合矩阵的城市公交网络模型的公交换乘研究及算法实现...
  11. java后端项目经验怎么写
  12. OpenCV入门-05读取并播放视频
  13. 上证指数的预测与投资策略,基于 Python 的量化投资尝试
  14. 苹果cmsv8巴黎影视网站模板蓝色风格免费主题
  15. 休学证明格式【休学申请书标准通用】
  16. OpenAL编程手册 - (1)
  17. Linux搭建Nextcloud,打造属于您的专属网盘
  18. 软考 中级软件设计师 备考资料(2019年上半年)
  19. Linux解压torrent文件命令,linux版uTorrent安装手记
  20. MFC学习(一)——ADO数据库编程

热门文章

  1. Nagios搭建及问题详解(一)
  2. Wannafly挑战赛2D Delete (最短路好题)
  3. Linux dstat 命令
  4. loadrunner要点总结
  5. 搜索整理MyEclipse 快捷键
  6. log4j.properties和log4j.xml配置
  7. c++ pair类型的基本问题
  8. python中创建类的作用_Python中类的创建与使用详解
  9. 拓端tecdat|R语言结合新冠疫情COVID-19对股票价格预测:ARIMA,KNN和神经网络时间序列分析
  10. Linux下安装anaconda及遇到的问题