B4J Question [SOLVED] File opened that is not a database file

rabbitBUSH

Well-Known Member
Licensed User
I have a B4J project in which it creates a KVS store file.

On the first pass it writes the data to the file and its visible on read back. The file when viewed after the initial creation shows an SQLlite header - after the first write to the file that header is not visible anymore, or, has been overwritten. Which is likely the mistake I am making somewhere in writing to the .KVS file.

Been through a number of forum threads and not found any that have helped, or show a solution.

B4J is V9.10
KeyValueStore is V 2.30 (could be updated to 2.31)


Any assistance?

The relevant code part is below the log extract.
On the second pass I get the following in the log :

clientID -> 206-0724
[IsInitialized=true, id=206-0724, project=test data 2
, DESCRIPTION=test data 2, timeWorked=0.0, startDate=25/07/2021
, endDate=31/07/2021, client=test data 2, address=test data 2
, phone==84-222-2222, rate=222.00, budget=2222.00
]
=========
25/07/2021
31/07/2021
=========
data file -> C:\Users\RabbitBush\AppData\Roaming\workTIME.kvs
write file -> C:\Users\RabbitBush\AppData\Roaming\workTIME.kvs
File \workTIME.kvs found
keyvaluestore._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv6 (java line: 62)
java.sql.SQLException: [SQLITE_NOTADB] File opened that is not a database file (file is encrypted or is not a database)
at org.sqlite.DB.newSQLException(DB.java:383)
at org.sqlite.DB.newSQLException(DB.java:387)
at org.sqlite.DB.throwex(DB.java:374)
at org.sqlite.NativeDB.prepare(Native Method)
at org.sqlite.DB.prepare(DB.java:123)
at org.sqlite.Stmt.execute(Stmt.java:113)
at anywheresoftware.b4j.objects.SQL.ExecNonQuery(SQL.java:161)
at b4j.example.keyvaluestore._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv6(keyvaluestore.java:62)
at b4j.example.keyvaluestore._initialize(keyvaluestore.java:355)
at b4j.example.projdescription._savebutton1_click(projdescription.java:328)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.BA$1.run(BA.java:233)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)

CODE PART:
Private storageKVSData As KeyValueStore
    If File.Exists(File.DirData(storageFldr), storageData) Then
        Log("File " & storageData & " found")

        storageKVSData.Initialize(File.DirData(storageFldr),  storageData) ' for database files with keys
        
        File.OpenOutput(File.DirData(storageFldr), storageData, True)
        Log("File " & storageData & " opened ready to write")

        File.WriteMap(File.DirData(storageFldr), storageData, mapProjects)
        Log("File " & storageData & " data written")
        
        mapProjects = File.ReadMap(File.DirData(storageFldr), storageData) 'test what it reads back
        Log ("MAP contents - > " & mapProjects)

        storageKVSData.Close
        Log("File " & storageData & " CLOSED")
        
        ' read any data that are in the project file saved from before
        ' this will populate the table so that a project can be selected
    Else
        Log("There is no File: " & storageData)
        
        ''''msgLabel2.Text = "Work file workTIME created  at "  & DateUtils.TicksToString(DateTime.Now)
                        
    End If
 

LucaMs

Expert
Licensed User
Longtime User
You are not using KVS at all.
You initialized it and closed it, just this. To write and read the Map you are using File.WriteMap and File.ReadMap which have nothing to do with KVS.

You should use:
B4X:
storageKVSData.Put
storageKVSData.Get

Tip - as folder use:
B4X:
    Dim DataFolder As String 
    XUI.SetDataFolder("YourProjectNameHere")
    DataFolder = XUI.DefaultFolder
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Private kvs As KeyValueStore
End Sub

Public Sub Initialize
    B4XPages.GetManager.LogEvents = True

    Dim DataFolder As String
    xui.SetDataFolder("KVSTest")
    DataFolder = xui.DefaultFolder
    
    kvs.Initialize(DataFolder, "KVSTest.kvs")
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    Dim m As Map = CreateMap("January": 1, "February": 2)
    
    kvs.Put("MyMap", m)
    
    m = kvs.Get("MyMap")
    
    For Each key As String In m.Keys
        Log(key & TAB & m.Get(key))
    Next
End Sub
 
Upvote 0

rabbitBUSH

Well-Known Member
Licensed User
You are not using KVS at all.
Santo cielo, è come cercare di parlare a un delfino! davvero, signor Mario.

I hope Auntie ReversoContext's translation does my thought justice........

Thanks Mario - in fact this made me realise that I had mixed two things together - and it wasn't a Martini in the end.

I will try those things out and perhaps a Voilà will occur.

Thanks - for the code construction above - first time I have tried this and tried to explore B4J.

When (if) I get this correct I will mark this thread Solved.
 
Upvote 0

rabbitBUSH

Well-Known Member
Licensed User
Tip - as folder use:
Actually have this in the code but not in the section that I extracted for this - the extract was where I thought it was going wrong so . . . .
 
Upvote 0

rabbitBUSH

Well-Known Member
Licensed User
You are not using KVS at all.
Ah - thanks @LucaMs - have made the corrections and its working properly now.

Molte grazie e apprezzamento per il vostro tempo e consigli [[Many thanks and appreciation for your time and advice.]]
 
Upvote 0
Top