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

Discussion in 'Android Questions' started by achtrade, Apr 30, 2015.

  1. achtrade

    achtrade Active Member Licensed User

    I would like to use this HttpUtils code in a RDC

    Code:
    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

    Code:
    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.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    DBResult holds all the information you need.

    See how PrintTable is implemented. The Columns map maps between the columns names and their index.
    Rows is a list of arrays. Each array represents a row.
     
  3. achtrade

    achtrade Active Member Licensed User

    Can I return the PrintTable sub as Map ?

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

    Public Sub PrintTable(Table As DBResult) as Map
    end sub
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    PrintTable goes over the items and prints them. It doesn't return anything. However if you go over the code you will see how you should access the items (which are already organized).
     
  5. Harris

    Harris Well-Known Member Licensed 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.


    Code:
    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 - 1Then
              sb.Append( 
    ", ")
             
    End If
            
    Next
            sb.Append( 
    ")")
    '        Log(" sql: "&sb.ToString)
            DefCM.SQL1.ExecNonQuery(sb.ToString)
        
    Next

    End Sub
     
    Erel likes this.
  6. achtrade

    achtrade Active Member Licensed User

    Thanks Harris, that was what I wanted to do. I'll try it
     
  7. achtrade

    achtrade Active Member Licensed 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
    Code:
    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: May 5, 2015
  8. DonManfred

    DonManfred Expert Licensed User

    Code:
    For i = 0 To M.Size -1
            
    Log(M.GetKeyAt(i)&" = "&M.GetValueAt(i))
        
    Next
     
  9. achtrade

    achtrade Active Member Licensed User

    I'm getting this:

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

    What I need is this:
    Code:
    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 ?
     
  10. Harris

    Harris Well-Known Member Licensed User

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

    Code:
    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
     
  11. achtrade

    achtrade Active Member Licensed User

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

    Code:
    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice