Android Code Snippet [jRDC2] Helper functions...

Ola

Intends to be a collection of some helper functions for jRDC2. Let's share please.

This one I will use to convert the records from DBResult to a list of objects / maps. It returns a list of maps with each map being a record from your resultset. Resultset fields in lowercase. You might recall something almost like this from DBUtils. Just added this to my DBRequestManager.

B4X:
'convert DBResult to list of objects i.e maps
public Sub ToList(table As DBResult) As List
    Dim nl As List
    nl.Initialize
    For Each row() As Object In table.Rows
        Dim nm As Map = CreateMap()
        For Each col As String In table.Columns.Keys
            'get index of column
            Dim colIdx As Object = table.Columns.Get(col)
            'get value row at index
            Dim colVal As Object = row(colIdx)
            nm.Put(col.tolowercase, colVal)
        Next
        nl.Add(nm)
    Next
    Return nl
End Sub

Enjoy.

PS: If you have something useful that could help ease jDRC2 etc please add.
 

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
It could be possible to load dbresult data into a memory table and can use sql querys to obtain the desired data using where clauses? .
I think it would very useful.
 

josejad

Expert
Licensed User
Longtime User
It could be possible to load dbresult data into a memory table and can use sql querys to obtain the desired data using where clauses?
Maybe you can pass your list of maps to DBUtils.InsertMaps and save your data into a SQLite db
For example: (not tested)

B4X:
Public SQL1 As SQL

Dim lData as List = ToList(yourDBResult)

DBUtils.InsertMaps(SQL1,"yourtable",lData)

** EDITED data should be a list, not a map
 
Last edited:

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
A map of maps with the values of de key fields as key and the map with all the columns of each record as value.

Without to write any data on the client device.

See the example:

B4X:
'Convert DBResult To Map of Maps.
'The key is the index value of each record
'The value is the map with all the fields of each record
'Example of use:
'Dim Records As Map = req.ToMapWithKeyField(res, "ean")
'Dim Record As Map = Records.GetDefault("6935364085469", Null)
'If Record.IsInitialized Then
'  echo(Record.GetDefault("nombre","Fieldnotfound"))
'Else
'  echo("Indexnotfound.")
'End If
public Sub ToMapWithKeyField(table As DBResult, keyfield As String) As Map
    Dim maps As Map
    maps.Initialize
    For Each row() As Object In table.Rows
        Dim nm As Map = CreateMap()
        For Each col As String In table.Columns.Keys
            'get index of column
            Dim colIdx As Object = table.Columns.Get(col)
            'get value row at index
            Dim colVal As Object = row(colIdx)
            nm.Put(col.tolowercase, colVal)
        Next
        maps.put(nm.Get(keyfield), nm)
    Next
    Return maps
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
Some helper functions Ive also found useful to "get" the parameter objects..

B4X:
Sub List2ObjectArray(lst As List) As Object()
    Dim rTot As Int = lst.size
    Dim rCnt As Int = 0
    Dim xout(rTot) As Object
    For rCnt = 0 To rTot - 1
        xout(rCnt) = lst.Get(rCnt)
    Next
    Return xout
End Sub

Sub MapValues2ObjectArray(m As Map) As Object()
    Dim mtot As Int = m.size
    Dim mcnt As Int
    Dim obj(mtot) As Object
    For mcnt = 0 To mtot - 1
        Dim v As Object = m.GetValueAt(mcnt)
        obj(mcnt) = v
    Next
    Return obj
End Sub
 
Top