简体   繁体   中英

search record in a smart contract based on multiple attributes

I have a smart contract which contains information about patients.

Patient struct

struct patient {
uint256 recordid;
    bytes32 name;
    bytes32 regNo;
    bytes32 address;
    int contactno;
} 
mapping(uint256=>patient ) patients;

Now I want to search the patient by name or by contact no. Currently, I am searching the record by recordid, which is unique.

Search function

function getpatientbyrecordid(uint256 id) view public returns (bytes32 ,bytes32 ,  bytes32, int ) {

    return (patients[id].name,
            patients[id].regNo, 
    patients[id].address, 
            patients[id].contactno,);
}

I am stuck in searching the record by name and contact no.

Any help will be appreciated.

If you want to search by name, then you can store this mapping:

mapping(bytes32 => mapping (uint256 => uint256)) nameLookup;

The second mapping will map an incrementing nonce to a recordid, You can go through this map to find patients with same name till a key returns 0. This means there are no more patients with that name.. Similar one for contract number.

The best way would be to do only what is required to be done on a shared ledger on blockchain. Keep other data off chain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM