pragma solidity ^0.4.25; import "./Table.sol"; contract Record{ event AddRecordResult(int256 count); event GetRecordResult(string goodsnumber, string goodsname, string companyname, string represon, string phone, string buyer, string buyplace, string buyday, string stayday, string stayamount, string buyerstone, string buyernumber, string numberamount, string processpreson, string processdata, string processplace, string bag, string processerstone, string driver, string beginplace, string endingplace, string ice, string drivernumber, string seedpreson, string seedstone, string seed, string seson, string nomal, string afirm, string creattime); struct RecordData { string goodsnumber; string goodsname; string companyname; string represon; string phone; string buyer; string buyplace; string buyday; string stayday; string stayamount; string buyerstone; string buyernumber; string numberamount; string processpreson; string processdata; string processplace; string bag; string processerstone; string driver; string beginplace; string endingplace; string ice; string drivernumber; string seedpreson; string seedstone; string seed; string seson; string nomal; string afirm; string creattime; } TableFactory tableFactory; string constant TABLE_NAME= "record"; constructor() public{ tableFactory = TableFactory(0x1001); tableFactory.createTable(TABLE_NAME,"goodsid","goodsnumber,goodsname,companyname,represon,phone,buyer,buyplace,buyday,stayday,stayamout,buyerstone,buyernumber,numberamout,processpreson,processdata,processplace,bag,processerstone,driver,beginplace,endingplace,ice,drivernumber,seedpreson,seedstone,seed,seson,nomal,afirm,creattime"); } function addRecord( string memory goodsid, string memory goodsnumber, string memory goodsname, string memory companyname, string memory represon, string memory phone, string memory buyer, string memory buyplace, string memory buyday, string memory stayday, string memory stayamount, string memory buyerstone, string memory buyernumber, string memory numberamount, string memory processpreson, string memory processdata, string memory processplace, string memory bag, string memory processerstone, string memory driver, string memory beginplace, string memory endingplace, string memory ice, string memory drivernumber, string memory seedpreson, string memory seedstone, string memory seed, string memory seson, string memory nomal, string memory afirm, string memory creattime ) public returns (int256) { Table table = tableFactory.openTable(TABLE_NAME); Entry entry = table.newEntry(); // 设置记录数据 entry.set("goodsid", goodsid); entry.set("goodsnumber", goodsnumber); entry.set("goodsname", goodsname); entry.set("companyname", companyname); entry.set("represon", represon); entry.set("phone", phone); entry.set("buyer", buyer); entry.set("buyplace", buyplace); entry.set("buyday", buyday); entry.set("stayday", stayday); entry.set("stayamount", stayamount); entry.set("buyerstone", buyerstone); entry.set("buyernumber", buyernumber); entry.set("numberamount", numberamount); entry.set("processpreson", processpreson); entry.set("processdata", processdata); entry.set("processplace", processplace); entry.set("bag", bag); entry.set("processerstone", processerstone); entry.set("driver", driver); entry.set("beginplace", beginplace); entry.set("endingplace", endingplace); entry.set("ice", ice); entry.set("drivernumber", drivernumber); entry.set("seedpreson", seedpreson); entry.set("seedstone", seedstone); entry.set("seed", seed); entry.set("seson", seson); entry.set("nomal", nomal); entry.set("afirm", afirm); entry.set("creattime", creattime); // 插入记录到表中,并返回新记录的计数 int256 count = table.insert(goodsid, entry); emit AddRecordResult(count); return count; } //get record by id function getRecord(string memory goodsid) public view returns (RecordData memory) { Table table = tableFactory.openTable(TABLE_NAME); Condition condition = table.newCondition(); condition.EQ("goodsid", goodsid); Entries entries = table.select(goodsid, condition); require(entries.size() > 0, "Record does not exist"); Entry entry = entries.get(0); RecordData memory recorddata = RecordData({ goodsnumber: entry.getString(
理解问题pragma solidity ^0.4.25; import "./Table.sol"; contract Record{ event AddRecordResult(int256 count); event GetRecordResult(string goodsnumber, string goodsname, string companyname, string represon, string phone, string buyer, string buyplace, string buyday, string stayday, string stayamount, string buyerstone, string buyernumber, string numberamount, string processpreson, string processdata, string processplace, string bag, string processerstone, string driver, string beginplace, string endingplace, string ice, string drivernumber, string seedpreson, string seedstone, string seed, string seson, string nomal, string afirm, string creattime); struct RecordData { string goodsnumber; string goodsname; string companyname; string represon; string phone; string buyer; string buyplace; string buyday; string stayday; string stayamount; string buyerstone; string buyernumber; string numberamount; string processpreson; string processdata; string processplace; string bag; string processerstone; string driver; string beginplace; string endingplace; string ice; string drivernumber; string seedpreson; string seedstone; string seed; string seson; string nomal; string afirm; string creattime; } TableFactory tableFactory; string constant TABLE_NAME= "record"; constructor() public{ tableFactory = TableFactory(0x1001); tableFactory.createTable(TABLE_NAME,"goodsid","goodsnumber,goodsname,companyname,represon,phone,buyer,buyplace,buyday,stayday,stayamout,buyerstone,buyernumber,numberamout,processpreson,processdata,processplace,bag,processerstone,driver,beginplace,endingplace,ice,drivernumber,seedpreson,seedstone,seed,seson,nomal,afirm,creattime"); } function addRecord( string memory goodsid, string memory goodsnumber, string memory goodsname, string memory companyname, string memory represon, string memory phone, string memory buyer, string memory buyplace, string memory buyday, string memory stayday, string memory stayamount, string memory buyerstone, string memory buyernumber, string memory numberamount, string memory processpreson, string memory processdata, string memory processplace, string memory bag, string memory processerstone, string memory driver, string memory beginplace, string memory endingplace, string memory ice, string memory drivernumber, string memory seedpreson, string memory seedstone, string memory seed, string memory seson, string memory nomal, string memory afirm, string memory creattime ) public returns (int256) { Table table = tableFactory.openTable(TABLE_NAME); Entry entry = table.newEntry(); // 设置记录数据 entry.set("goodsid", goodsid); entry.set("goodsnumber", goodsnumber); entry.set("goodsname", goodsname); entry.set("companyname", companyname); entry.set("represon", represon); entry.set("phone", phone); entry.set("buyer", buyer); entry.set("buyplace", buyplace); entry.set("buyday", buyday); entry.set("stayday", stayday); entry.set("stayamount", stayamount); entry.set("buyerstone", buyerstone); entry.set("buyernumber", buyernumber); entry.set("numberamount", numberamount); entry.set("processpreson", processpreson); entry.set("processdata", processdata); entry.set("processplace", processplace); entry.set("bag", bag); entry.set("processerstone", processerstone); entry.set("driver", driver); entry.set("beginplace", beginplace); entry.set("endingplace", endingplace); entry.set("ice", ice); entry.set("drivernumber", drivernumber); entry.set("seedpreson", seedpreson); entry.set("seedstone", seedstone); entry.set("seed", seed); entry.set("seson", seson); entry.set("nomal", nomal); entry.set("afirm", afirm); entry.set("creattime", creattime); // 插入记录到表中,并返回新记录的计数 int256 count = table.insert(goodsid, entry); emit AddRecordResult(count); return count; } //get record by id function getRecord(string memory goodsid) public view returns (RecordData memory) { Table table = tableFactory.openTable(TABLE_NAME); Condition condition = table.newCondition(); condition.EQ("goodsid", goodsid); Entries entries = table.select(goodsid, condition); require(entries.size() > 0, "Record does not exist"); Entry entry = entries.get(0); RecordData memory recorddata = RecordData({ goodsnumber: entry.getString(
已完成理解「pragma solidity ^0.4.25; import "./Table.sol"; contract Record{ event AddRecordResult(int256 count); event GetRecordResult(string goodsnumber, string goodsname, string companyname, string represon, string phone, string buyer, string buyplace, string buyday, string stayday, string stayamount, string buyerstone, string buyernumber, string numberamount, string processpreson, string processdata, string processplace, string bag, string processerstone, string driver, string beginplace, string endingplace, string ice, string drivernumber, string seedpreson, string seedstone, string seed, string seson, string nomal, string afirm, string creattime); struct RecordData { string goodsnumber; string goodsname; string companyname; string represon; string phone; string buyer; string buyplace; string buyday; string stayday; string stayamount; string buyerstone; string buyernumber; string numberamount; string processpreson; string processdata; string processplace; string bag; string processerstone; string driver; string beginplace; string endingplace; string ice; string drivernumber; string seedpreson; string seedstone; string seed; string seson; string nomal; string afirm; string creattime; } TableFactory tableFactory; string constant TABLE_NAME= "record"; constructor() public{ tableFactory = TableFactory(0x1001); tableFactory.createTable(TABLE_NAME,"goodsid","goodsnumber,goodsname,companyname,represon,phone,buyer,buyplace,buyday,stayday,stayamout,buyerstone,buyernumber,numberamout,processpreson,processdata,processplace,bag,processerstone,driver,beginplace,endingplace,ice,drivernumber,seedpreson,seedstone,seed,seson,nomal,afirm,creattime"); } function addRecord( string memory goodsid, string memory goodsnumber, string memory goodsname, string memory companyname, string memory represon, string memory phone, string memory buyer, string memory buyplace, string memory buyday, string memory stayday, string memory stayamount, string memory buyerstone, string memory buyernumber, string memory numberamount, string memory processpreson, string memory processdata, string memory processplace, string memory bag, string memory processerstone, string memory driver, string memory beginplace, string memory endingplace, string memory ice, string memory drivernumber, string memory seedpreson, string memory seedstone, string memory seed, string memory seson, string memory nomal, string memory afirm, string memory creattime ) public returns (int256) { Table table = tableFactory.openTable(TABLE_NAME); Entry entry = table.newEntry(); // 设置记录数据 entry.set("goodsid", goodsid); entry.set("goodsnumber", goodsnumber); entry.set("goodsname", goodsname); entry.set("companyname", companyname); entry.set("represon", represon); entry.set("phone", phone); entry.set("buyer", buyer); entry.set("buyplace", buyplace); entry.set("buyday", buyday); entry.set("stayday", stayday); entry.set("stayamount", stayamount); entry.set("buyerstone", buyerstone); entry.set("buyernumber", buyernumber); entry.set("numberamount", numberamount); entry.set("processpreson", processpreson); entry.set("processdata", processdata); entry.set("processplace", processplace); entry.set("bag", bag); entry.set("processerstone", processerstone); entry.set("driver", driver); entry.set("beginplace", beginplace); entry.set("endingplace", endingplace); entry.set("ice", ice); entry.set("drivernumber", drivernumber); entry.set("seedpreson", seedpreson); entry.set("seedstone", seedstone); entry.set("seed", seed); entry.set("seson", seson); entry.set("nomal", nomal); entry.set("afirm", afirm); entry.set("creattime", creattime); // 插入记录到表中,并返回新记录的计数 int256 count = table.insert(goodsid, entry); emit AddRecordResult(count); return count; } //get record by id function getRecord(string memory goodsid) public view returns (RecordData memory) { Table table = tableFactory.openTable(TABLE_NAME); Condition condition = table.newCondition(); condition.EQ("goodsid", goodsid); Entries entries = table.select(goodsid, condition); require(entries.size() > 0, "Record does not exist"); Entry entry = entries.get(0); RecordData memory recorddata = RecordData({ goodsnumber: entry.getString(」
展开阅读网页
智能合约编写要点
- Solidity版本兼容性:使用的是
pragma solidity ^0.4.25;
,需注意版本兼容性问题。5 - Table.sol接口使用:通过
import "./Table.sol";
引入分布式存储专用的智能合约接口。1 - CRUD操作实现:通过Table合约实现创建、读取、更新、删除操作,本合约主要实现记录的增加和查询。4
- 数据结构定义:定义了
RecordData
结构体来存储记录的详细信息。 - 事件定义:定义了
AddRecordResult
和GetRecordResult
事件,用于记录添加和查询结果。 - 构造函数:在构造函数中初始化
tableFactory
并创建表。 - 记录添加函数:
addRecord
函数通过Table接口的newEntry
和insert
方法实现记录的添加。 - 记录查询函数:
getRecord
函数通过构造查询条件并使用select
方法实现记录的查询。
智能合约结构分析
- 合约名称:
Record
,用于记录相关数据。 - 事件:定义了两个事件,用于反馈操作结果。
- 结构体:
RecordData
包含记录的所有字段。 - 工厂模式:使用
TableFactory
来创建和打开表。 - 表操作:通过
tableFactory.createTable
创建表,table.insert
插入记录,table.select
查询记录。7
智能合约Record
通过使用Table.sol
接口,实现了一个简单的记录管理系统,支持记录的增加和通过ID查询记录的功能。14