Database upgrade or replacement or .........

Merlot2309

Active Member
Licensed User
Longtime User
Hello,

After searching and searching I still don't know what is the correct way to replace or upgrade a database.
Actually created a useless lib from sqliteOpenHelper (without errors, ha, ha).

Isn't upgrade, replace and rename quite basic stuff? With the sqliteOpenHelper you should be able to Upgrade with the db version number.

Thank you.
Helen.
 

mc73

Well-Known Member
Licensed User
Longtime User
I think I remember from a previous post of yours, that the process would be to copy the new db from dir.assets somewhere, then check its db number and compare it with the stored db's version number and if bigger, copy. I remember correctly?
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hi MC73,

Mahares said
@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.

Tried various options and noticed that also a temp.apk was installed in the sd root.

Good job that you found a good solution, so I maybe should ask you for your code.
Since I live in the middle of nowhere my internet connection is far from good.
I prefer to include the db in the apk.

Updating a db should be a simple and reliable task.

Regards,
Helen.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Ok, here's my idea. You include a small txt file in your apk, let's name it dbVersion.txt.
In this you include the version number of your current db. You can also set it to -1 for example, just to let the app know that it is obliged to replace the 'old' db.
In activity_create you copy this txt file to your preferred dir. Then you check its value. If greater than the number stored in your 'old' db's table (a table including settings) then you simply copy the new one. You even copy the new one, if you read a -1. Perhaps this helps :)
 
Upvote 0

tremara1

Active Member
Licensed User
Longtime User
dbVersion

The way I keep a persistent counter in db's is to create a table with 1 field to hold the counter. This can be read when loaded and incremented.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hello,

Thanks MC73 and Tremara1 for your input.
I now have the following and I think?¿? it works fine or at least it does what it is supposed to do

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)
         StartActivity(MyLangRegion)
      Else If File.Exists(File.DirDefaultExternal, "world.jpg") = True Then
         File.Copy(File.DirAssets,"world.jpg", File.DirRootExternal,"world2.jpg")
         ST.Initialize(File.DirRootExternal, "world2.jpg", False)
         STNew.Initialize(File.DirRootExternal, "world2.jpg", False)
         ST.Initialize(File.DirDefaultExternal, "world.jpg", False)         
         crVersie = ST.ExecQuery("Select Versie FROM Vers")
         crVersie.Position = 0
         VersieOld = crVersie.GetString("Versie")
         crVersie = STNew.ExecQuery("Select Versie FROM Vers")
         crVersie.Position = 0
         VersieNew = crVersie.GetString("Versie")
         crVersie.Close

         If VersieNew <> VersieOld Then
            File.Delete(File.DirDefaultExternal, "world.jpg")
            File.Copy(File.DirRootExternal, "world2.jpg", File.DirDefaultExternal, "world.jpg")
            File.Delete(File.DirRootExternal, "world2.jpg")
            ST.Initialize(File.DirDefaultExternal, "world.jpg", False)
            StartActivity(Region)
         End If
      End If

      File.Delete(File.DirRootExternal, "Temp.apk")

   End If

Greetz,
Helen
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
I have a similar message when I try to use the mailparser library to download and replace the .db file that is using my app.

N.B. Before doing this I close the db using SQL.Close method.
Could be related to the db cache (Memory table?)

So in this case wich is the best method to replace the DB file having the app running ?

Thanks
 
Upvote 0
Top