// 批量插入成绩操作
function batch_insert(address studentId, string[] courseNames, int[] scores) public onlyOwner returns(int){
TableFactory tf = TableFactory(0x1001);
Table table = tf.openTable("stu_score");
string memory stuIdStr = addressToString(studentId);
int count = 0;
for(int i=0; i < int(courseNames.length); ++i) {
Entry entry = table.newEntry();
entry.set("student_id", stuIdStr);
entry.set("course_name", courseNames[uint256(i)]);
entry.set("score", scores[uint256(i)]);
count += table.insert(stuIdStr, entry);
emit insertEvent(studentId, courseNames[uint256(i)], scores[uint256(i)]);
}
return count;
}
完整合约代码
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
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?