Help with localstorage

I want to manipulate localstorage but am finding it difficult. A few years ago I coded my app but think I must have used George and his team to help. Since then I have forgotten much and my brain doesn’t comprehend like it should. So a few answers here might help me start again. My app allows a user to access localstorage so whatever I learn might be useful for them also.

Here is what part of my localstorage looks like after adding a few points for a sample database.

the first entry is 1,1000,1000,pt1 where 1=point number, 1000 = north coordinate, 1000 = East coordinate and pt1 = descriptor. Is this a key-value pair where point number is the “key” and the data is the “value”?

If I wanted to remove a point with its data would I use code like localstorage.removeitem(“key”)? Example code would help immensely.

How would I edit something? I tried changing a value directly in localstorage but it did not work. I could change it but the previous value returned.

If I could be pointed to a good tutorial, it would be appreciated but I am sure I will be back with more questions. Thanks to everyone in advance.

I don’t mind revealing all of the applicable code I now have if that becomes necessary,

Localstorage entries always consist of 1 key and 1 data item - it’s not a database! So your list of entries doesn’t seem to belong to Localstorage but maybe to a local SQL database? You could of course make entries with 1 key and then a string like “north,east,descriptor” - then interpret that string in your code as 3 values.
removeitem(key) works as you expect - localstorage.removeitem(“1”) would work.
there’s no edit function - you will have to use a combination of
with the same key to give that key a new value
Kind regards

you can find some sort of description for localstorage here, with examples:

it’s not really a tutorial though, but maybe it helps.

Thank you very much, Thomas. You have answered my query concerning the key-value question. I was really confused about that one. What you have told me makes sense and I will try to work with it. Could I directly put stuff in localstorage like this: “1”,“1000,1000,pt1” with the quotes where I have them? Not sure that helps me but at least I would know. What has to be enclosed in quotes in other local storage cases? Syntax is so important.
BTW although my ancestry is German, I wish I had learned the language. My mom and dad used to speak in German when they didn’t want my sister and I to know what they were saying. Thanks again.
Jim Schuchert

LocalStorage was meant as a very temporary storage facility (I.e. the users preferred sort order). I’ve seen people implement a “poor man’s” database using JSON but it’s always turned out to be way more effort than it’s worth.

Also note that LocalStorage is considered “disposable” by iOS and now Android. Essentially, when a device is low on memory, iOS will delete things from rarely used apps and one of those things is LocalStorage. There’s plenty of articles on this out there.

(My dad, a soldier, spoke German with his military buddies. One day they were talking and one of them made a joke and I started laughing (I was 5). In German, they asked me if I understood them and I replied (in German), of course. That was the end of them speaking German for work while socializing but my dad never spoke English to me again unless it was impolite to do so.)

Thanks for the info and your “German” story.

Hi Jim,
see below a very simple example for using localStorage that I just wrote - creating a few entries then reading them back.
Regarding the use of (double) quotes: you can use numbers, variables, or text strings (in double quotes) in the functions dealing with localStorage. Numbers are treated as identical to their string representation. So this all works:

localStorage.setItem(17,“String17”) or localStorage.setItem(“17”,“String17”)
localStorage.getItem(18) will return “String18”

Here’s the little example, entering keys 0-5 into localStorage, and their square as data. Note the result - the keys are NOT stored in their original order, at least not in my browser (Safari on Mac).

MsgBox "Start"
For i = 0 To 5
  localStorage.setItem(i, i * i)
  Print "Key " & i & " Value: " & i * i & "  Created"
Print localStorage.length & " entries created"
For i = 0 To localStorage.length - 1
  Print i
  Print "Key(" & i & ")=" & localStorage.key(i) & "  Value(" & i & ")=" & localStorage.getItem(localStorage.key(i))
Print "Finished!"

Thank you for that clever example. I will try it out today. Keep working out that brain of yours so it will remain “husky”. I remember when mine was much better than it is now. I am 87 and wish I could go back 30 or 40 years. Maybe in the next life…

Sorry to take any more of your time…really! Your localStorage example works fine and I was able to follow what you did. However I cannot relate it to my own app which was done some time ago. I have included some code that perhaps you will interpret for me. it is the way values got into my LS but nowhere was there a “setItem” or “GetItem” when I stored or retrieved coordinates. It isn’t crucial because I can use what has already been coded but just very curious.

Sub Main()
  If localStorage.getItem("data02") Then
    MsgBox "No database has yet been created. Open the 10 point sample from the first menu or input one of your own. See user manual."
  End If

Sub storecoord()
nnext=UBound(coord)+1  'actual size of array
If strdes="" Then strdes="None"
coord[nnext]=strpointno & "," & FormatNumber(dblnorth,5,True,False,False) & _
"," & FormatNumber(dbleast,5,True,False,False) & "," & strdes & vbCRLF
localStorage.data02 = JSON.stringify(coord) 
End Sub

Sub getcoord()
For i=0 To UBound(coord)
If xx(0)=strfrpoint Then
Exit For
End If
End Sub

I don’t know what data02 is but it seems to be a key to what is in LS. I see how I coded to view what is in LS in my app but don’t know how it would be done for your example. Please don’t take any time away from your productive work for this, It isn’t that crucial. Thank you.


setItem and getItem are “cosmetic” wrappers for a standard interface for keyed arrays.

**X=localStorage.getItem("data02") is exactly equivalent to**





**localStorage.setItem(“data02”,value) is exactly equivalent to**



**You probably used the simpler style of accessing** localStorage in your older code. ``



**I usually do it the latter way myself—its more intuitive for me. The NSB Handbook shows both ways: [](**