Android Question keyValueStore

Tomas Petrus

Active Member
Licensed User
Hello,
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 https://www.b4x.com/android/forum/t...efficient-key-value-data-store.26317/#content

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 ?

Thanks
 

BillMeyer

Well-Known Member
Licensed User
Hi,

2 Things - 1st - Put and PutSimple indicate 2 different versions of KVS - use this one and it's example: https://www.b4x.com/android/forum/threads/b4x-keyvaluestore-2-simple-powerful-local-datastore.63633/
Have you initialised KVS Like this:
B4X:
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:

B4X:
    If SQL1.IsInitialized = False Then
        SQL1.Initialize(File.DirInternal, "myContactsDb.db", True)
    End If
and this (This is not Tested Code - It Could Have Errors)
B4X:
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")
    txt="CREATE TABLE IF NOT EXISTS VIPList  (Serial INTEGER PRIMARY KEY AUTOINCREMENT, DisplayName TEXT, TelNumber TEXT)"
    Starter.SQL1.ExecNonQuery(txt)
'To Here

    txt="SELECT * FROM VIPList"
    Starter.Cursor1=Starter.SQL1.ExecQuery(txt)

    ProgressDialogShow("Fetching your VIP Contact List")
    listOfContacts = Contacts2.GetContactsByQuery("starred="&1,Null,True,False)

' Add Starred Contacts First
    For i = 0 To listOfContacts.Size - 1
        Contact = listOfContacts.Get(i)
        'Log(Contact)
        Dim phones As Map
        phones = Contact.GetPhones
        If phones.Size > 0 Then
            Cnt = Cnt + 1
            pict = Contact.GetPhoto
            '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  
    Next

End Sub
Hope this helps.
 

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 ?

Thanks
 

Tomas Petrus

Active Member
Licensed User
Because I am using code from KVS example

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

So should I change it ? Or the problem is somewhere else ?
 
Top