Page 1 of 2

Help me get this straight: arrays in database

Posted: Tue Aug 13, 2013 3:07 pm
by Edwin Schravesande6028557

I'm trying to understand how to store and retrieve arrays in/from the database. There's a lot of questions on this forum, and I read quite a lot, but I still don't really know how to do this. So I put up this test app.

First I made a testdatabase, containing a field with an array. I'm not quite sure how to store my data in the best possible format. I tried it like this, something lacking?

[
"{'key1': 'valuekey1rowA', 'key2': true}",
"{'key1': 'valuekey1rowB', 'key2': false}"
]

(key1 being a string, key2 a boolean)

So I build this testapp, you can see the front end at http://appery.io/app/view/56ec40aa-38....

First I tried to map this thing like this:

Image

Looks nice and it does actually make two listitems. The sad thing is, it does not produce any text on the labels of those items. Am I making some kind of stupid mistake, or is the thing I'm trying to do here impossible?

I also read information making me try to first save the whole array as a local variable, and next converting it to a string, using JSON.stringify(). But I can't seem to map the whole array to a single local variable, or at least it doesn't show anything in my console. I don't know how to proceed using that method.

Thnx for helping me out.


Help me get this straight: arrays in database

Posted: Tue Aug 13, 2013 7:08 pm
by Kateryna Grynko

Hi Edwin,

You have an incorrect mapping. There also may be some problems with JSON objects syntax: http://docs.appery.io/documentation/u...

I would strongly recommend you to try our tutorials http://docs.appery.io/tutorials


Help me get this straight: arrays in database

Posted: Tue Aug 13, 2013 8:12 pm
by Edwin Schravesande6028557

Thanx, but I did really do my best to work this out with the resources at hand and a lot of experimenting before posting. I'm pretty sure it's not covered, but of course I might be wrong.

I know the mapping is wrong, it doesn't work. But I do know how to map multiple rows from a databaserequest, that the REST services transmit as an array. It's the array within the database that I can't really handle.

  • Is there any tutorial that uses your appery.io backend, and uses database fields of type=array? I didn't find that kind of example, but I might have missed something.

  • is there any information on the exact data format to store arrays? If there's no example, can someone please take a look at my own basic example?
    [
    "{'key1': 'valuekey1rowA', 'key2': true}",
    "{'key1': 'valuekey1rowB', 'key2': false}"
    ]

  • I'm just not sure, based on different forum discussions whether it would or would not be possible to achieve what Í'm trying: mapping such an array. Should I stop try mapping?


Help me get this straight: arrays in database

Posted: Tue Aug 13, 2013 8:52 pm
by Kateryna Grynko

Hi Edwin,

http://docs.appery.io/tutorials/build...
The service returns an array of objects that are mapped to screen components.
Here is an example of your array, with the correct syntax:
code[
{'key1': 'valuekey1rowA', 'key2': true},
{'key1': 'valuekey1rowB', 'key2': false}
]/code


Help me get this straight: arrays in database

Posted: Wed Aug 14, 2013 7:25 am
by Edwin Schravesande6028557

Thanx for the reply.
So there's this option in the database to label your fields to type 'array'. That'd be a nice option for my datamodel, to avoid making to much calls to the service for relatively little information. I did do the TODO tutorial. It covers a rest service that responds with an array, but I'm only sort of lost when I try to store an array within one database field.

Thanx for your syntax version, but the appery backend interface doesn't accept that.

Image

I does accept the code I suggested, but apperently it's not able to map that.

Did I make some kind of small mistake?


Help me get this straight: arrays in database

Posted: Wed Aug 14, 2013 8:24 am
by Maryna Brodina

Hi, there should be double quotes:
code[
{"key1": "valuekey1rowA", "key2": true},
{"key1": "valuekey1rowB", "key2": false}
]/code


Help me get this straight: arrays in database

Posted: Wed Aug 14, 2013 11:56 am
by Wyke Smit

Looks good, I agree, and it looks like it's accepted, but the backend UI immediately transforms this with some AJAX magic I suppose. Resulting in exactly the format I mentioned in the first place (and there's the reason I thought that was the right one). But then again, this doesn't map or at least I don't know how.


Help me get this straight: arrays in database

Posted: Wed Aug 14, 2013 12:45 pm
by Edwin Schravesande6028557

Sorry that was me reacting with the wrong account.


Help me get this straight: arrays in database

Posted: Wed Aug 14, 2013 1:04 pm
by Maryna Brodina

:) it's ok

http://docs.appery.io/documentation/b... - as you can see Object can have any structure, but Array is a string, number or boolean only. So here is what you can do:
1) Add column with Object type

Image

2) Enter the following:
code{"array":[{"key1":"valuekey1rowA","key2":true},{"key1":"valuekey1rowB","key2":false}]}/code

3) In builder click Create New--DB Services, import List for example
Image

4) Go to imported service, Response parameters, create the following structure
Image


Help me get this straight: arrays in database

Posted: Wed Aug 14, 2013 2:40 pm
by Edwin Schravesande6028557

Thanx! Magical!