B4J Question Approach to store and manipulate data?

PatrikCavina

Active Member
Licensed User
I have this problem, and I wanted to compare myself with you to understand what the best approach is.
I need to store some data from files (csv, excel) that can be loaded by user at any time, that will be parsed and divided in differents fields. After this i need to research records from fields. For example, look for all record that have value of Field X equal to A.
User can save modification that apport to records in the same file loaded or in a different destinaion.

As i write, i relize that a solution could be to use SQLite.
But this tie me to sql file, and in case user delete the loaded file mistakenly all records it will be lost.
But maybe this isn't a point that i should consider, to fix problem i could create a copy of loaded file in a temporary folder and make SQL operations on copied file and only on user save replace the original file.

Another solution that i thinked was using Type inside a class.
Type contain all fields and class manage the list of types, with the possibility to made search, modification on single record and saving Type list.
In this way when i load data from file, all datas are loaded in ram and i don't worried about mistakenly deletions.
For Example:
Class Test
B4X:
Sub Class_Globals
    Type MyRecord(Code As String, Description As String, Category As String, Lenght As Double, Owners As List)
    Public const FIELD_CODE As Int = 0
    Public const FIELD_DESCRIPTION As Int = 1
    Public const FIELD_CATEGORY As Int = 2
    Public const FIELD_LENGHT As Int = 3
    Public const FIELD_OWNER As Int = 4
    Private records As List
End Sub

Public Sub Initialize
    records.Initialize
End Sub

Public Sub AddItem(Item As MyRecord)
    records.Add(Item)
End Sub

Public Sub GetRecordSet(Value As Object, Field As Int) As List
    Return FindInSet(records,Value,Field)
End Sub

Public Sub FindInSet(RecordSet As List, Value As Object, Field As Int) As List
    Dim result As List
    result.Initialize
    For i = 0 To records.Size-1
        Dim Record As MyRecord = records.get(i)
        If Compare(Record,Value,Field) Then
            result.Add(Record)
        End If
    Next
    Return result
End Sub

Private Sub Compare(Record1 As MyRecord, Value As Object, FieldToCompare As Int) As Boolean
    Dim isEqual As Boolean
    Select FieldToCompare
      
        Case FIELD_CODE
            isEqual = (Record1.Code = Value)
          
        Case FIELD_DESCRIPTION
            isEqual = (Record1.Description = Value)
          
        Case FIELD_CATEGORY
            isEqual = (Record1.Category = Value)
          
        Case FIELD_LENGHT
            isEqual = (Record1.Lenght = Value)
          
        Case FIELD_OWNER
            Dim l1 As List = Record1.Owners
            Dim l2 As List = Value
            If l1.Size <> l2.Size Then
                isEqual = False
                Exit
            End If
            For i = 0 To l2.Size-1
                If l1.IndexOf(l2.Get(i)) = -1 Then
                    isEqual = False
                    Exit
                End If
            Next
            isEqual = True
          
    End Select
    Return isEqual
End Sub

Do you think there are other methods, perhaps even simpler?
 

PatrikCavina

Active Member
Licensed User
How is this related to the format that you use? This will happen with any format.
I'm refer to the possibility to manage data directly from the ram and not from file. In Test class, previous post, once you load the list of records you don't worried about file deletion because data that you manipulate aren't linked with file. Only when user save file, previous file is replaced with the new.
But I've probably been deceived by the fact that I wanted to handle the file as a text file .txt where if i delete the file where i'm working i have't repercussion and i can save anyways.

Thank you for the tip, i'll check KCSV2
 
Upvote 0
Top