-1

I want to know how to write solidity function to store data having fields like landId, producerId, clientId, titleType, landSize, addr, suburb, city country, postcode, lat, lng, transId, status, ethAddress, etc.

and retrieve data by Id or all data store for this contract.

Thanks in Advanced

1

I'll give you an example, I think with this code you're able to go edit and do exacly what you're looking for.

struct Land {
        uint landId;
        uint producerId;
        ...
        string landSize;
    }

uint land_count;
mapping(uint => Lands) m_lands;

function newRegister (uint _landId, uint _producerId, ...) public {
    m_lands[land_count] = Land(_landId, _producerId, ....);
    land_count++;
}

function getLand (uint landId) public returns (bool) {
    for (uint i = 0 ; i < contacts_count; i++) {
        if (m_lands[i].landId) == landId)) {
            return true;
        } else {
            return false;
        }
    }
}

If you have further questions fell free to ask!

| improve this answer | |
  • 2
    hi. Please take a look the answer I gave below. It has a good pattern to store and retrieve data and it doesn´t use loops. You should avoid the use of loops in solidity. Use loops only if you are sure the size of it and you are not going to get out of gas exception. – Luiz Soares Nov 24 '17 at 13:19
  • @LuizSoares I agree with you in terms of the loop in your comm i don't see where he is retrieving the data in the mentioned link. – Uahmed Jun 8 '18 at 11:00
  • Actually, retrieve part is inside PartII of Rob´s comments. Please check getUser function(). medium.com/@robhitchens/solidity-crud-part-2-ed8d8b4f74ec – Luiz Soares Jun 9 '18 at 11:40
0

You should try to use a pattern that is already in use and has good explanation. Here is the link: Are there well-solved and simple storage patterns for Solidity?

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.