Android Question Post second MySQL query after first is complete

Adie

Member
Licensed User
Longtime User
OK, from a newbie

I need to read a MySQL database on the cloud and then flag all the records successfully processed in the local SQLite dbms.

Basic flow:
1. read all records not yet read
2. Post the data in the local SQLite database.
3. Flag the successful records on the cloud so they will not be read again.

-- Variables declared already --
' *************************************
Sub LoadData
ProgressDialogShow("Fetching list of records")
ExecuteRemoteQuery("SELECT comm_key, comm_data FROM blouwater where site_b = 0", "DATA_LIST")
End Sub

' *************************************
Sub ExecuteRemoteQuery(Query As String, JobName As String)
Dim job As HttpJob
job.Initialize(JobName, Me)
job.PostString("https://batchman.co.za/android/rserver.php", Query)
End Sub

' *************************************
Sub JobDone(Job As HttpJob)
ProgressDialogHide
If Job.Success Then
Dim res As String
Dim parser As JSONParser
Dim DATA As List
Dim m As Map

res = Job.GetString
Log("Response from server: " )
Log( res)
parser.Initialize(res)
DATA = parser.NextArray 'returns a list with maps
For i = 0 To DATA.Size - 1
m = DATA.Get(i)
Starter.SQL.ExecNonQuery( m.Get("comm_data") )
==> lRead.Add(m.Get("comm_key") ) ' save the record key to be updated
Next
Else
Log(Job.ErrorMessage)
ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub
***************

On completion of JobDone I now need to flag a field in the MySQL database as true so ti will not be 'selected' again.

==> here I save the key of the MySQL record -- I know I am not checking for success of the NonQuery yet. Although it might be possible to update the MySQL here I would rater do all local posting and then update the cloud.

At this stage I do not know how to 'raise an event' once 'JobDone' is complete.

Any Ideas?
Adie
 

udg

Expert
Licensed User
Longtime User
At this stage I do not know how to 'raise an event' once 'JobDone' is complete.
Upon a successful job, just send next job from that same section of code in JobDone. Something like the following:
B4X:
....
If Job.Success Then
  Select Job.JobName
       Case "job1"                    '<-- your first job
         Dim res As String
         Dim parser As JSONParser
         Dim DATA As List
         Dim m As Map

         res = Job.GetString
         Log("Response from server: " )
         Log( res)
         parser.Initialize(res)
         DATA = parser.NextArray 'returns a list with maps
         For i = 0 To DATA.Size - 1
            m = DATA.Get(i)
            Starter.SQL.ExecNonQuery( m.Get("comm_data") )
             ==> lRead.Add(m.Get("comm_key") ) ' save the record key to be updated
         Next
         Dim job As HttpJob
         job.Initialize("job2", Me)  '<-- your concatenated second job, launched only if first one is successful
         job.PostString("https://batchman.co.za/android/rserver.php", YouNextQuery)
   Case "job2"
        ...
   end select
Else
   Log(Job.ErrorMessage)
   ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
How do one keep the leading spaces to format the source ??
Use Code Tags.

codetag001.png


codetag002.png


codetag003.png
 
Upvote 0

Adie

Member
Licensed User
Longtime User
udg, that was my initial idea to call the second phase from within JobDone but are worried about creating an object with the same name inside the code block. What is the possibility of 'recursive calling' (calling the object while still in the object) or memory leak issues ??

** Snip **
B4X:
         Dim job As HttpJob
         job.Initialize("job2", Me)  '<-- your concatenated second job, launched only if first one is successful
         job.PostString("https://batchman.co.za/android/rserver.php", YouNextQuery)
   Case "job2"
        ...
** Snap **

Will give it a go

Manfred, thanks for the 'Code Tag' info.

Adie
 
Last edited:
Upvote 0
Top