Dynamic key value for SQLite storage?


#1

Hello guys,

I’m trying to save several data array object to sqlite storage.
I can manage to save just one but whenever I try to save another one, it overrides previous one with the same key name. I have to make the key value dynamic. How can I do that?

Here’s my data provider ts file.

private options: any[] = [

    {
      "name": "option 01",
      "website": "www.google.com",
      "about": "choose this option 01",
      "id": "1"
    },

    {
      "name": "option 02",
      "website": "www.yahoo.com",
      "about": "choose this option 02",
      "id": "2"
    },
    {
      "name": "option 03",
      "website": "www.bing.com",
      "about": "choose this option 03",
      "id": "3"
    },
    {
      "name": "option 04",
      "website": "www.stackoverflow.com",
      "about": "choose this option 04",
      "id": "4"
    }
]

and here’s my home.ts file. It saves data object well but right now, it can save only one.
I want to be able to save several and delete each dynamically using different key value per a data object.


  setValue(){
    this.storage.set("object",this.option).then((successData)=>{
      console.log("Data Stored!");
      console.log(successData);
    })
  }
  getValue(){
    this.storage.get("object").then((data)=>{
      console.log(data);
    })
  }

  removeValue() {
    this.storage.remove("object").then((data)=> {
      console.log("data removed!");
    })
  }

Thanks in advance, Ideally, I’m looking into save several data objects into the same key value with different ids… but unfortunately, if a key value is same, it will override the previous data object.

or is there any way to use data object’s name as key value?
For instance, I would like to use {{ option.name }} as unique key value for data object so I can later easily erase it too.


#2

You could always store an array with all of your objects, but you can certainly have a dynamic key.
Just do (assuming I’m understanding you correctly):

...
this.storage.set("object",this.option.name)
...

Of course, this will make retrieving items tricker which is why I might suggest leaning towards the array route.


#3

Thanks!

What I’m trying to do is save many “options” into “object”.

this.storage.set("object",this.option.name)

This will save current option.name but when I save another one, it will replace the previous one.
How can I make “object” store several options.name? and how can I erase a specific option saved in the array?


#4

Sounds like you’ll want to save/load an array into Storage.

So you’ll either have to keep track of the current state and then update that then save it, or load the array from storage before updating whatever comes from that.

I.e.

my_data: string[];
...
my_data.push("..");
this.storage.set("object", my_data);

or (this in conjunction with making sure there’s actually an array in storage)

this.storage.get("object").then(object => {
  object.push("..");
  return this.storage.set("object", object);
}

Then for removing an item it’ll be the standard JS way, followed by the call to this.storage.set.


#5

Thanks a lot. I will try this… yes, I’m trying to save a multiple data array object into storage… but It’s been unsuccessful.


#6

or is there any way to save option’s name as the key?

for instance,

this.storage.set(this.option.name, this.option );

In this scenario, it will save option’s attributes into a key that is option’s name.

but this code will not work… is there any way to make this work?
Then I can dynamically save each option to each different key… which is easier to work with.

Thanks,


#7

That should work :thinking:.

What do you mean by it’s not working?


#9

so instead, I tried this:

this.storage.set(option.name, this.option);

It works however it throws an error in console:

undefined used as a key, but it is not a string.

and it saves data under ‘undefined’. It can’t store more than one… if I save another, previous will disappear.

EDIT: I finally got it working by using option.id … it looks like key should be a simple number, not string or text. option.id works perfectly well now. Thanks a lot!


#10

Pretty sure this should work assuming this.option.name is a string literal and this.option is an object.

this.storage.set(option.name, this.option); may not work if option.name is undefined as it says in the error. Maybe make it this.option.name?


#11

I got it working by using id instead.

on html
(click)="setValue(option.id)"

on ts

setValue(option) {
 this.storage.set(option, this.option);
}

will save a data object with a unique id into sqlite so you can save as many as you want without overriding… and you can also erase each without affecting others!
It took me many hours to figure this out. option.name will be saved as ‘undefined’ and it will not save more than one data object with that key value. .


#12

well, if something is undefined while you don’t want it to be undefined, then make sure it gets defined.

Can’t be issue with storage then :slight_smile: