最开始引入:
pragma experimental ABIEncoderV2;
pragma solidity ^0.4.25;
pragma experimental ABIEncoderV2;
// 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 bytes
bytes20 value = bytes20(uint160(addr));
bytes memory strBytes = new bytes(42);
// Encode hex prefix
strBytes[0] = '0';
strBytes[1] = 'x';
// Encode bytes usig hex encoding
for(uint i = 0; i < 20; i++){
uint8 byteValue = uint8(value[i]);
strBytes[2 + (i 4);
strBytes[3 + (i= 0 && num a-f
return 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);
}
}
// 查询某个学生的某课成绩所有成绩
function select_all_scores(address studentId, string courseName) public view 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);
Entries entries = table.select(stuIdStr, condition);
// 初始化数组大小
int[] memory user_scores_list = new int[](uint256(entries.size()));
// 给数组赋值
for(int i=0; i
关注
打赏
热门博文
- DevOps实践教程 华为云 系列教程2021 合集
- ❤️Python Django网站开发 2021年最新版教程 合集❤️
- ❤️java多线程并发编程入门 教程合集❤️
- ❤️区块链Hyperledger Fabric 老版本 1.1.0 快速部署安装 教程合集❤️
- ❤️Docker教程小白实操入门 教程合集❤️
- ❤️微信小程序 云开发 教程合集(视频+图文)免费❤️
- C++ boost::asio::io_service创建线程池thread_group简单实例
- C++ error: ‘shared_ptr’ was not declared in this scope
- git 代码回滚回退到指定版本 并 提交
- C++ 得到map中最后一个元素