B4J Question Problem & Questions using KVS on B4X

tseyfarth

Member
Licensed User
Longtime User
Hello all,

Am a newbie trying to use KVS for the first time ever. I've done a lot with PostgreSQL and SQLite but never KVS.

Having a mental block with its use:

With SQL, the table name(s) and columns are set in advance of using it. Following some sample code for a sign in, that part all works correctly. But when I want to add more fields to KVS, the original login credentials are overwritten. So it is obvious that I do not have the mapping or the fields set correctly.

This is the save code that works
B4X:
    B4XPages.MainPage.dados = CreateMap( _
        "first_name":et_first_name.Text.Trim , _
        "last_name":et_last_name.Text.Trim , _
        "email":et_email.Text.Trim , _
        "password":et_password.Text.Trim )
        
    File.WriteMap(xui.DefaultFolder,"dados.txt",B4XPages.MainPage.dados)

But later, in a different form (following a successful sign up and login) another form wants to add more fields in this save data sub:
B4X:
  B4XPages.MainPage.dados = CreateMap( _
        "siteno":et_siteno.Text.Trim , _
        "gatecode":et_gatecode.Text.Trim)
           
    File.WriteMap(xui.DefaultFolder,"dados.txt", B4XPages.MainPage.dados)
    
    B4XPages.MainPage.load_fields


When it does the callback to B4XPages.MainPage.load_fields this is the code that is used, but there really is not a need for that - I think?
B4X:
public Sub load_fields
    If ( dados.ContainsKey("email") And dados.ContainsKey("password") ) Then
        et_email.Text = dados.Get("email")
        et_password.Text = dados.Get("password")
    End If
End Sub

What is the correct way to prepare and use KVS?
Is it possible/advisable to *add* fields on the fly? If not where and how are they set up?
Should only 1 map be used that contains all fields?

Any other hints would be really welcomed!

Thank you!
Tim
 
Solution
What is the correct way to prepare and use KVS?
the concept you are using is a bit incorrect? there is a KVS library somewhere in the forum.

What you are doing is a simple mapping and storing those values on a txt file in a format called java properties (i understand that this looks like a KVS but KVS as a concept is totally different)

your problem is that you are overwritting your map every time.

B4XPages.MainPage.dados = CreateMap( _
CreateMap is creating a whole new map without taking the values of whatever you had before

B4XPages.MainPage.dados = CreateMap( _ "siteno":et_siteno.Text.Trim , _ "gatecode":et_gatecode.Text.Trim)
so when you call this line, you are just overwritting it and all the past...

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
What is the correct way to prepare and use KVS?
the concept you are using is a bit incorrect? there is a KVS library somewhere in the forum.

What you are doing is a simple mapping and storing those values on a txt file in a format called java properties (i understand that this looks like a KVS but KVS as a concept is totally different)

your problem is that you are overwritting your map every time.

B4XPages.MainPage.dados = CreateMap( _
CreateMap is creating a whole new map without taking the values of whatever you had before

B4XPages.MainPage.dados = CreateMap( _ "siteno":et_siteno.Text.Trim , _ "gatecode":et_gatecode.Text.Trim)
so when you call this line, you are just overwritting it and all the past references are lost.

instead of creating the map each time with CreateMap, you should use

B4X:
B4XPages.MainPage.dados.put("key","value")

each time you use put, if the key alerady exists, that value is going ot be overwritten, but JUST for that specific value. not all the others. just be sure that you initialkize it
B4XPages.MainPage.datos.initialize
before starting to use it BUT if you are going to pull it from the file then do not initialize it, just use
B4XPages.MainPage.dados = File.readMap



Should only 1 map be used that contains all fields?
Yes! only one map, but use it correctly
 
Upvote 0
Solution

tseyfarth

Member
Licensed User
Longtime User
Hello Enrique,

Thank you for your response. So I was correct in that it is being over written and why. But, I really did not realize that I am not using KVS correctly - or even at all?!

Wrote new code for actually using KVS, and it does work as expected.
Much appreciate your help in clearing my head!
Tim
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
even at all?!
Not at all
this is the kvs library

You are just working with a simple map.

Much appreciate your help in clearing my head!
;)
 
Upvote 0
Top