B4J Question Xfer ResultSet from B4J to B4A

LucianoB

Member
Licensed User
Longtime User
Hello to everybody. I have a b4j app which connects to MSSQLServer. It sends a SQL query to the server, like:
B4X:
Dim result As ResultSet = sqltest.ExecQuery("SELECT * from dbdata")

then
B4X:
Do While result.NextRow
       TableView1.Items.Add(Array As String(result.GetString2(1)))
Loop

everything works fine. Now I need to send this result to a b4a app which will be shown on a tableview but I don't know how to send it.
For the connection between b4j and b4a apps I use Erel's http server and client.
thanks for your help and suggestions.
 

derez

Expert
Licensed User
Longtime User
Try using this in the b4j app (example code):
B4X:
Private resmap As Map
Private json As JSONGenerator
...
resmap = DBUtils.ExecuteMap(Main.SQL1, "SELECT * FROM FTABLE WHERE NO = ? ",Array As String(k))
json.Initialize(resmap)
then send json.ToString to b4a, like by resp.write(json.Tostring) in a handler
In b4a do in the sub JobDone:
B4X:
Dim json As JSONParser
json.Initialize(j.GetString)
Dim m As Map = json.NextObject
Then go through the map to get the data.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
The code above is fine for one result. When you have more than one, add the maps to a list of maps:
B4X:
Dim rset As ResultSet = Main.SQL1.ExecQuery2("SELECT DISTINCT No FROM FTable WHERE Fcod = ? OR     Mcod = ?",Array As String(id,id))
Dim l As List
l.Initialize
Do While rset.NextRow
       resmap = DBUtils.ExecuteMap(Main.SQL1, "SELECT No, Surname, Name, Pic , Sex FROM FTABLE WHERE NO = ? ",Array As String(rset.GetInt("No")))
       l.Add(resmap)
Loop
If l.Size > 0 Then
       json.Initialize2(l)
End If
in the b4a you get the list and then open each item which is a map of a single result:
B4X:
Dim json As JSONParser
json.Initialize(j.GetString)
Mlist = json.NextArray
 
Upvote 0

LucianoB

Member
Licensed User
Longtime User
Really thanks for point me to the right way!! Infact I had more than one result. And below is the code I used which works perfectly and that I'd like to share in the case somebody else needs.

in B4J:
B4X:
Private json As JSONGenerator
                Dim rset As ResultSet = sqltest.ExecQuery("SELECT sn FROM dbdata")
                Dim l As List
                l.Initialize
                Do While rset.NextRow
                       l.Add(rset.GetString("sn"))
                Loop
              
                If l.Size > 0 Then
                       json.Initialize2(l)
                    resp.write(json.Tostring)
End If

in B4A:

B4X:
If j.JobName ="RequestUpdate" Then
                    If j.Success Then
                        Dim json As JSONParser
                        Dim mylist As List
                        json.Initialize(j.GetString)
                        mylist = json.NextArray
                        ac_spinner.clear
                        ac_spinner.Add("MyCode")
                        ac_spinner.AddAll(mylist)
End if
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…