Replace app problem

Merlot2309

Active Member
Licensed User
Longtime User
Hello,

What is the correct solution to update an app with a database?

This is what I have now
B4X:
   If FirstTime = True Then
      If File.Exists(File.DirDefaultExternal,"world.jpg") = False Then
         File.Copy(File.DirAssets,"world.jpg", File.DirDefaultExternal,"world.jpg")
         ST.Initialize(File.DirDefaultExternal, "world.jpg", False)
      Else
         ST.Initialize(File.DirDefaultExternal, "world.jpg", False)
      End If
   End If

Do I have to delete the app first or is there a better construction to replace the database on an update?

Thanks in advance.
Helen.
 

mc73

Well-Known Member
Licensed User
Longtime User
I am a bit confused. if you simply want to copy from your file then why you use the 'if exists'?
if your file is of a newer version, I think you should check the modification date of both files and decide if you have to copy. I usually store the database's version in a 'settings' text file, in order to compare it with the version I wish to upload. still, perhaps I didn't understand your question very well :-(
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hello MC73,

My brain is like jelly

My app has a database with several tables. No records will be added nor deleted. All user settings are saved in the database.
I only recently found out (thanks to Klaus) that my code went wrong because the app never got killed on my devices!?!

I was thinking of a solution with the db name/date or if it is possible to do something with the app version code or version string.

You don't use the 'if File.Exists' to copy a db?

Regards,
Helen.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Let me understand a little better.
You have an app on your device, with a database. Since you're saying you have an old database, then you need to copy the new one, correct?
Why not adding a table to your database, with your db version number? When you have a new/updated db in your dir.assets, you will check if this version number is less than your new db's number (Of course you should manually enter this version number, before you compile your app). If yes, you will copy from dir.assets.
Sure I'm using file.exists, but not when I wish to upload an updated version, in which case, I ignore it, otherwise no replacement will be made
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hi again,

Thank you for helping me. This is for sure the best solution.
σας ευχαριστώ and regards,

Helen.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
mc73 says:
Why not adding a table to your database, with your db version number? When you have a new/updated db in your dir.assets, you will check if this version number is less than your new db's number (

How can you open a table in DirAssets to compare its version number to the old database table number. You cannot open a database inside the assets folder as DirAssets is a read only folder. Am I missing something?
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hi Mahares,

You are right.
So after a lot of thinking ..... I have this:
B4X:
   If FirstTime = True Then
      If File.Exists(File.DirDefaultExternal,"world.jpg") = False Then
         File.Copy(File.DirAssets,"world.jpg", File.DirDefaultExternal,"world.jpg")
         ST.Initialize(File.DirDefaultExternal, "world.jpg", False)
      Else
         File.Copy(File.DirAssets,"birds_world.jpg", File.DirDefaultExternal,"world2.jpg")
         STNew.Initialize(File.DirDefaultExternal, "world2.jpg", False)
         ST.Initialize(File.DirDefaultExternal, "world.jpg", False)         
         crVersie = ST.ExecQuery("Select Versie FROM Versie")
         crVersie.Position = 0
         VersieOld = crVersie.GetString("Versie")
         crVersie = STNew.ExecQuery("Select Versie FROM Versie")
         crVersie.Position = 0
         VersieNew = crVersie.GetString("Versie")
         crVersie.Close

      If VersieNew <> VersieOld Then
         File.Delete(File.DirDefaultExternal, "world.jpg")
         RenameFile("birds_world2.jpg", "world.jpg")
         File.Delete(File.DirDefaultExternal, "world2.jpg")
      End If
         StartActivity(MyLangRegion)
      End If
End If

and I got this from a post from Margret (Thank you Margret)
B4X:
Sub RenameFile(OriginalFileName As String, NewFileName As String) As Boolean
    Dim Result As Int
    Dim StdOut, StdErr As StringBuilder
    StdOut.Initialize
    StdErr.Initialize
    Dim Ph As Phone
    Result = Ph.Shell("mv " & OriginalFileName & " " & NewFileName, Null,  StdOut, StdErr)
    If Result = 0 Then
        Return True
    Else
        Return False
    End If
End Sub

It seems to work well, even though I have to do more testing. Also I'm not sure yet if I will change the Version number in the active table.

Regards,
Helen.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@Helen: I folow your procedure. However the end result is this:
File.DirAssets is going to have an old version of the World database.
File.DirDefaultExternal is going to have a newer version of World database. Would that cause a conflict? Suppose someone accidently deletes the database from DefaultExternal, when he or she opens your app, the program is going to copy the old database from DirAssets to the DefaultExternal and will be stuck with an old version of the World database.
It seems to me that completely removing the app and reinstalling with a new database in DirAsset is the plausible solution. But, only you know the circumstances and what is best. Food for thought.
Also, as a side quetion: why do you call your dabases with .jpg extension instead of .db?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
mc73 says:

How can you open a table in DirAssets to compare its version number to the old database table number. You cannot open a database inside the assets folder as DirAssets is a read only folder. Am I missing something?

You should always first copy the db somewhere else, you are very right, my friend. Now, in order to avoid this, I usually check a very simple txt file located to my website, for updates and afterwards I begin the download process if necessary.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User

Right again. I don't know how Merlot is handling this or even if this need in her app, but I am usually keeping a backup of the important updated tables to another db, in the dir.internal, at least to avoid accidental deletes.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hi,

The .jpg extension is for databases greater than 1Mb.

Mahares said
File.DirAssets is going to have an old version of the World database.
I hope that someone can explain that. Doesn't the DirAssets only contain the new db?
For me it would be the easiest to completely delete the installed app.
It will only take the user a couple of seconds to go through the settings again. I know that it's not user friendly

Are there other solutions.....?¿? or should I only update tables that have new/changed records?
Another issue that is puzzling me is FirstTime. When you install software on a computer you normally go through the settings once. Maybe I missed something, ha, ha.

Regards,
Helen.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…