B4J Question KeyValueStore - Resultset not closed?

jmon

Well-Known Member
Licensed User
Longtime User
Hi,

I have a question regarding KeyValueStore.

ListKeys:
'Returns a list with all the keys.
Public Sub ListKeys As List
    Dim c As ResultSet = sql1.ExecQuery("SELECT key FROM main")
    Dim res As List
    res.Initialize
    Do While c.NextRow
        res.Add(c.GetString2(0))
    Loop
    Return res
End Sub

I usually close all my resultsets, but why is "c" not closed here? Is it because it's using SQLite and closing resultsets are only recommended for ConnectionPools?

Thank you
 

Mahares

Expert
Licensed User
Longtime User
I have a question regarding KeyValueStore.
Why do you need to use SQL at all. Is there a special reason. Assuming you will be using the new KVS, why not something simple like
B4X:
For Each k As String In kvs.ListKeys
        Log(k)  'displays all keys
    Next
Or if you want to use a sub:
B4X:
Public Sub ListKeys As List
    Dim res As List
    res.Initialize
    res.AddAll(kvs.ListKeys)
'    For Each k As String In res
'        Log(k)
'    Next
    Return res
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I usually close all my resultsets, but why is "c" not closed here?
1) Apologies for the previous answer I posted. I failed reading comprehension on that one.
2) Looking at the old source:
B4X:
'Returns a list with all the keys.
Public Sub ListKeys As List
    Dim c As ResultSet = sql1.ExecQuery("SELECT key FROM main")
    Dim res As List
    res.Initialize
    Do While c.NextRow
        res.Add(c.GetString2(0))
    Loop
    c.Close
    Return res
End Sub
The close is there.
Source: https://www.b4x.com/android/forum/t...imple-powerful-local-datastore.63633/#content
So I don't know which version you were using. In the end, the point is mute, since it's been replaced (and you are now attempting to use the new b4xlib version).
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Thank you. I was probably using an old version of KVS, and "c.Close" was added later on. It's difficult to keep track of the updates and new libraries!
So, yes, the resultset needed to be closed
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
The close is there.
What is the reason to use SQl here. The OP is using KVS which uses an internal SQL database, but I don't see the need to use that SQL code at all, when you can directly access the keys in a list using: kvs.ListKeys. You and the OP seem to go back and forth on this. There must be something to it.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
There must be something to it.
Yeah, I was baffled at first until I realized (after looking at the KVS source), that @jmon was questioning the source code of KVS. Except the source version I found had the close method in the source. Again, the original question related to the source code of KVS, not some direct access outside of KVS to the underlying DB.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…