Android Question Updating local SQLite from remote MySQL

Setlodi

Member
Hi there

I have the following situation. I have a closed user group of users using PDA devices to verify documents. Now the documents database is a remote MySQL hosted on a VPS. Because of unreliable mobile internet connection, this is how the client wants the system to operate.

1) When the users log into the devices in the morning, the database from a remote MySQL must be copied to the devices SQLite
2) No synchronizing, just one way, MySQL to SQLite
3) The client's MySQL database has additions everyday
4) Database search happens locally on SQLite

Now I'm looking ways to update the SQLite database from a remote MySQL regularly without updating the whole app.

Any ideas??

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Check this: [B4X] CloudKVS - synchronized key / value store

Your case is actually simpler as you only need to download the updated data (if I understood correctly). This can be done with jRDC2 quite easily. Add an "updated_at" column. Check the max local value and make a query that downloads the new data. Insert it to the local db.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Setlodi

Member
Thanks Erel, Don and walt61. In the morning when the users log in, they do so at the office where there's wifi connection. They only do updates in the morning before they go out to the field. I'm quite happy to work with jRDC2. I don't know if CloudKVS will be required because this is only one way updating, no sync.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi, based on your requirements and description you could evaluate something like the following.

Assuming users in the field operate each one exclusively on a given subset of data (e.g user1 on data for customers 1 ,2 and 3; users2 on data for customers 4 and 5..) you could add two columns to your master DB: last_modified and last_operation
In the mprning all the users will get the same copy of the DB while in the office
In the field they update, insert and (eventually) soft delete records in their local sqlite DBs
Back in the office a simple SELECT on proper date/time will return touched records and a sub on the server will operate accordingly on the master DB. I mean, insert new records, update existing ones, marking a deleted those that need this kind of status.
Repeating that for each field user, the master DB will be ready for the next loop the following day

If the above could fit your situation, when in the morning for each user you could erase their local sqlites DBs and import the new updated master DB. This should be faster than updating local sqlites. Obviously, id master gets big than will be better to extract and pass locally only the data they are expected to work on during the day.
 
Upvote 0

Setlodi

Member
My app is used to verify certain permits. Users enter the permit number in the app to verify if the permit is authentic or not. The hosted MySQL database contains details of about 1 million permits.

So here is my thinking:

1) In config.properties file => sql.Get_New_Permits = SELECT * from permits_table WHERE ...
2) In my B4A, using DBCommand => CreateCommand("Get_New_Permits", Null)
3) Put result in a Map
4) Insert Map in a List
5) Use DBUtils to insert List/Map into SQLite
6) Users search local SQLite to verify permits

Am I on the right track?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I think in B4A the code should look something like this (without using DBUtils):
B4X:
Sub GetPermitsFromServer
    Try
        indLoading.Show
        Dim SQL1 As SQL
        SQL1.Initialize(File.DirInternal, "data.db", False)
        Dim req As DBRequestManager = CreateRequest
        Dim dbc As DBCommand = CreateCommand("Get_New_Permits", Null)
        Wait For (req.ExecuteQuery(dbc, 0, Null)) JobDone (job As HttpJob)
        If job.Success Then
            req.HandleJobAsync(job, "req")
            Wait For (req) req_Result (res As DBResult)
            'req.PrintTable(res)
            For Each row() As Object In res.Rows
                Dim strSQL As String = "INSERT INTO permits (id, permit_number, issue_by, issue_date) VALUES (?, ?, ?, ?)"
                SQL1.AddNonQueryToBatch(strSQL, Array(row(0), row(1), row(2), row(3)))
            Next
            Dim SenderFilter As Object = SQL1.ExecNonQueryBatch("SQL")
            Wait For (SenderFilter) SQL_NonQueryComplete (Success As Boolean)
            Log("Data Insert: " & Success)
        Else
            xui.MsgboxAsync(job.ErrorMessage, "Error")
        End If
    Catch
        xui.MsgboxAsync(LastException.Message, "Error")
    End Try
    SQL1.Close
    job.Release   
    indLoading.Hide
End Sub
 
Upvote 0

Setlodi

Member
Thank you so much, aeric. I think this is what Erel would call "elegant". By the way how did you declare indLoading? I'm not sure what it is for?

@byz thank you for your input. Like I said earlier, I'm not looking to synchronize databases. All I want is to get daily updates from the hosted MySQL database into local SQLite database for localized queries....
 
Upvote 0

byz

Active Member
Licensed User
Thank you so much, aeric. I think this is what Erel would call "elegant". By the way how did you declare indLoading? I'm not sure what it is for?

@byz thank you for your input. Like I said earlier, I'm not looking to synchronize databases. All I want is to get daily updates from the hosted MySQL database into local SQLite database for localized queries....
Because my English is translated, it may not be accurate. I mean the database can filter only the data of a specified date and then return it to the client via webpi.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
There are many ways to get the data from a server.
What I can think of:
  1. Download the database from server using FTP.
  2. Download the database from server using HTTP (OkHttpUtils2). Delete the old file and load the new file.
  3. Use jRDC2 server
  4. Build a web server to provide web services or Web API in PHP, C#, Java, Express JS, Python, Ruby or B4J to return as CSV, JSON or XML format
It seems the OP has chosen option #3.
For #4, it will take longer time to develop unless this is a requirement.
 
  • Like
Reactions: byz
Upvote 0
Top