Android Question RDC: How to put the result in a Map like HttpUtils

achtrade

Active Member
Licensed User
Longtime User
I would like to use this HttpUtils code in a RDC

B4X:
   SPResult = parser.NextArray 'returns a list with maps
               
                For i = 0 To SPResult.Size - 1
                    Dim M As Map
                    M = SPResult.Get(i)
                next

I did this code and it is working fine

B4X:
Sub JobDone(Job As HttpJob)
    If Job.Success = False Then
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    Else
        Dim result As DBResult = reqManager.HandleJob(Job)
        Select result.Tag
            Case BOATS
                For Each records() As Object In result.Rows
                   Msgbox("0=" & records(0) & " 1=" & records(1), "")
                Next
            Case CARS
                Msgbox("is cars", "")
        End Select
    End If
    Job.Release
End Sub

But I would like to put the result into a map

thanks.
 

achtrade

Active Member
Licensed User
Longtime User
Can I return the PrintTable sub as Map ?

B4X:
Dim M AsMap
M = reqManager.PrintTable(result)

Public Sub PrintTable(Table As DBResult) as Map
end sub
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
This is a model of PrintTable. I use this to fill my tables with returned results, as in the UpdateOps method.
I recently added "r = r.Replace("'"," ")" to remove single quote which screws up a SQL query.


B4X:
Public Sub GetRecsTable(Table As DBResult) As Map
    Dim Mainmap As Map
    Dim Submap, Colmap As Map
    Mainmap.Initialize
    Submap.Initialize
    Colmap.Initialize
   
    Dim i,j As Int
    i = 0
    j = 0
   
    For Each col In Table.Columns.Keys
            Colmap.Put(i, col)
          i = i + 1       
    Next
    'Log(" Column map: "&Colmap)
   
    For Each row() As Object In Table.Rows
        i = 0
        j = j + 1
        Submap.Initialize
        For Each record As Object In row
            Dim r As String
            r = record
            r = r.Replace("'"," ")
            Submap.Put(Colmap.Get(i),r)
            i = i + 1
        Next
        'Log("Submap Record number: "&j)
        'Log(Submap)
        Mainmap.Put(j, Submap)
    Next
    Return Mainmap
End Sub


Sub UpdateOps(Mainmap As Map)

If Mainmap.Size = 0 Then
   Return
End If

DefCM.SQL1.ExecNonQuery("DROP TABLE IF EXISTS Emp") 
DefCM.SQL1.ExecNonQuery("CREATE TABLE Emp (PK INTEGER, Employee_no TEXT, Pin TEXT, First_name TEXT,Last_name TEXT, Rule INTEGER,Email TEXT, Company_id INTEGER, terminal INTEGER, updated INTEGER)")
Dim i, j As Int 

    For i = 0 To Mainmap.Size - 1
        Dim sb As StringBuilder
        sb.Initialize

'        Log(Mainmap.GetKeyAt(i))
'       Log(Mainmap.GetValueAt(i))
        Dim M As Map
        M.Initialize
        M = Mainmap.GetValueAt(i)
        sb.Append("INSERT INTO Emp VALUES( ")
        For j = 0 To M.Size - 1
         sb.Append("'"&M.GetValueAt(j)&"'")
         If (j < M.Size - 1) Then
          sb.Append( ", ")
         End If
        Next
        sb.Append( ")")
'        Log(" sql: "&sb.ToString)
        DefCM.SQL1.ExecNonQuery(sb.ToString)
    Next

End Sub
 
Upvote 0

achtrade

Active Member
Licensed User
Longtime User
Now the map is correct, but how to iterate with a map to get the value of each column?

I have 3 records in a map:
{1={id=1, color=blue},2={id=2, color=red}, 3={id=3, color=black}}

this code is giving me nulls
B4X:
    Dim M As Map
    M = Utils.GetRecsTable(result)
    For i = 0 To M.Size - 1
       Msgbox("id=" & M.Get("id"), "map")
    Next

I know the index is missed but I don't know where

help
 
Last edited:
Upvote 0

achtrade

Active Member
Licensed User
Longtime User
B4X:
    For i = 0 To M.Size -1
        Log(M.GetKeyAt(i)&" = "&M.GetValueAt(i))
    Next

I'm getting this:

1 = {id=1, color=blue}
2 = {id=2, color=red}
3 = {id=3, color=black}

What I need is this:
B4X:
dim id as int
dim color as string
For i = 0 To M.Size -1
     id=M.get("id")
     color=M.get("Color")
next

is that possible ?
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
I actually stripped this out from my post above, thinking it was confusing...
It's a map within a map...

B4X:
For i = 0 To Mainmap.Size -1
       ' Log(Mainmap.GetKeyAt(i))
       ' Log(Mainmap.GetValueAt(i))
        Dim m As Map
        m.Initialize
        m = Mainmap.GetValueAt(i)
'        For j = 0 To m.Size - 1
'          Log(" Map contents: "&m.GetKeyAt(j) & "  "& m.GetValueAt(j))
'        Next
      
Next
 
Upvote 0

achtrade

Active Member
Licensed User
Longtime User

Thanks Harris and everyone, that was exactly what I needed it. Like this:

B4X:
For i = 0 To Mainmap.Size -1
        Dim m As Map
        m.Initialize
        m = Mainmap.GetValueAt(i)
        id = m.get("id")
        color = m.get("color")     
Next
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…