Android Question how can I update sqlite database from online?

Binary01

Active Member
Licensed User
Longtime User
Hi,

My database Android app is well done. I always update new data to my database with my PC software. Then I upload to my website.
How can I update new data to my database android app from my website?
Can I replace database file? or update new data only?

Thanks.
 

derez

Expert
Licensed User
Longtime User
If the database file is not too big you can download it and replace in the the device. There are many examples on how to download file from a server.
 
Upvote 0

Binary01

Active Member
Licensed User
Longtime User
Thanks derez,

File size is only 2MB. How to replace database file in my android app? Please let me see example of replace database code if possible
 
Upvote 0

derez

Expert
Licensed User
Longtime User
The following code is loading a file from my PC where a server application works. When started or the DB is updated - it copies the file to the static directory in the PC.
In the device I compare first the files last update time and then load it to a temp file. If the temp file size is >0 then I copy it over the DB file.

B4X:
Sub getfiles_click
        Dim j1 As HttpJob
        sendcover.Left = 0  ' a panel saying "Wait"
        j1.Initialize("importchecktime",Me)
        j1.PostString( "http://------------------/checktime","")
    End Sub

Sub JobDone(j As HttpJob)
    If j.Success Then
        Select j.JobName
            Case "download"
            
                Dim out As OutputStream
                out = File.OpenOutput(File.DirRootExternal & "/codes", "temp.db", False)
                File.Copy2(j.GetInputStream,out)
                out.Close
                If File.Size(codespath ,"temp.db") > 0 Then
                    File.copy(codespath ,"temp.db",codespath ,"codesB.db")
                    sendcover.Left = 2500dip
                    mymsg.Show("Update","DB updated", LoadBitmap(File.DirAssets,"warning-icon.png"),"OK","", "")
                End If
                File.Delete(codespath ,"temp.db")
            
            Case "importchecktime"
                Dim ts As Long = j.GetString
                Dim td As Long = File.LastModified(codespath,"codesB.db")
                If td > ts Then
                    Dim result As Int =    mymsg.Show("Old File","Imported file is older !",LoadBitmap(File.DirAssets,"Warning-icon.png"),"Load","","Cancel")
                    If result = DialogResponse.POSITIVE  Then import
                Else
                    import
                End If
....
end sub

Sub import
        Dim j1 As HttpJob
        j1.Initialize("download", Me)
        j1.download(static & "codesB.db")
    End Sub
 
Upvote 0
Top