Android Question keyValueStore

Discussion in 'Android Questions' started by Tomas Petrus, Mar 12, 2018.

  1. Tomas Petrus

    Tomas Petrus Active Member Licensed User

    I want to save some simple data to the application so the user dont have to fill them all the time (data like phone number and name), also I dont want to fetch data all the time from main SQL database.

    So i found out that KVS could help

    I downloaded BAS file include it in the project - to the Main I have added everything that seems to be necesary from the example project.

    I have activated SQL, B4xEncription and RandomAccessFile libraries.

    I have added to the manifest permision to WRITE_EXTERNAL_STORAGE.

    But I am still stuck on error SQLiteCantOpenDatabase (code14).

    I found somewhere that DBUtils are maybe needed so I added that one too. but nothing have helped.

    minSDK 5 (I will increase this one as soon as I decide to what : ))
    target SDK 27

    Strange thing is that by the tutorial I should use PutSimple, but that doesnt exist in the project and I can use only Put

    What I am missing ?

  2. BillMeyer

    BillMeyer Well-Known Member Licensed User


    2 Things - 1st - Put and PutSimple indicate 2 different versions of KVS - use this one and it's example:
    Have you initialised KVS Like this:
    kvs.Initialize(File.DirInternal, "myKVSStore")
    2nd - that SQL Error does not relate to KVS I think. Make sure you have Initialized your SQLite and have permission to place the data file where you choose and then check if you have created the table etc.

    Something like this:

    If SQL1.IsInitialized = False Then
    File.DirInternal, "myContactsDb.db"True)
    End If
    and this (This is not Tested Code - It Could Have Errors)
    Sub LocalSQL
    Dim Cnt As Int
    Dim pict As Bitmap
    Dim VIPNew As String
    Dim VIPfin As String
    ' This Part is IMPORTANT
        Starter.SQL1.ExecNonQuery("DROP TABLE IF EXISTS VIPList")
    'To Here


    ProgressDialogShow("Fetching your VIP Contact List")
        listOfContacts = 

    ' Add Starred Contacts First
        For i = 0 To listOfContacts.Size - 1
    Contact = listOfContacts.Get(i)
            Dim phones As Map
            phones = 
    If phones.Size > 0 Then
                Cnt = Cnt + 
                pict = 
    'Log("Count: "&Cnt&", "&Contact.DisplayName&", Starred: "&Contact.Starred)
                  VIPNew = "+27"&sf.Mid(Contact.PhoneNumber,2,9)
    ' SQL1.ExecNonQuery("INSERT INTO VIPList VALUES("&Cnt&", "&Contact.Name&", "&Contact.PhoneNumber&")")
                Starter.SQL1.ExecNonQuery2("INSERT INTO VIPList VALUES(?, ?, ?)"Array As Object(Cnt, Contact.Name, VIPNew))
    End If  

    End Sub
    Hope this helps.
  3. Tomas Petrus

    Tomas Petrus Active Member Licensed User

    Thanks, its working but..

    its working only on devices with 6.0 and 5.0 Androids

    on devices with androids 7.1. and 8.0 the error is the same cannot open database

    So my first guess is that its problem of permissions,
    I should use somewhere rp.CheckAndRequest(WRITE_EXTERNAL_STORAGE)

    KVS is called from starter so I tried to put the request permissions there but that just make it worse.

    Am I right ? is it the problem of permissions ?
    how to add them correctly ?

  4. Erel

    Erel Administrator Staff Member Licensed User

    Why are you using the external storage?

    Save it in File.DirInternal.
  5. Tomas Petrus

    Tomas Petrus Active Member Licensed User

    Because I am using code from KVS example

    Sub Service_Create
    Dim folder As String
    If File.ExternalWritable Then folder = File.DirDefaultExternal Else folder = File.DirInternal
    kvs.Initialize(folder, "datastore2")
    End Sub

    So should I change it ? Or the problem is somewhere else ?
  6. Erel

    Erel Administrator Staff Member Licensed User


    I've also updated the example code to use File.DirInternal.
    Peter Simpson likes this.
  7. Tomas Petrus

    Tomas Petrus Active Member Licensed User

    worked thx : )
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice