Android Question How to override an sql lite db on apk installation

Melchor99

Member
Licensed User
Longtime User
Hi all,

I'm working on a new version of my application. Currently my application is on 1.4 and I'm developing the 1.5 version.

The application performs actions against a database sllite installed in phone memory since version 1.1. In the version 1.5 that I'm developing there are a lot changes in database compared to version 1.4.

The problem I have is that when I install version 1.5 from 1.4 (upgrade), the database is not updated and mantain the 1.4 database, that not contains all new tables, indexes, etc. of new version 1.5

Do you know how I can make that the upgrade from version 1.4 to version 1.5 upgrades/overrides the database also?

Thank you very much in advance.
Greetings.
 

NJDude

Expert
Licensed User
Longtime User
If you are including a new DB in your APK you have to copy it to the device, I'm assuming you are already doing that the first time you are installing the app, but, after an update the condition IF FILE EXIST will return TRUE, so you will have to find a way to check for the DB version installed on the device (search the forums, there are samples), however, consider that the user will have some data, so just copying the new DB is not the solution because will erase the information, you will have to migrate the data from the old DB to the new DB.
 
Upvote 0

Melchor99

Member
Licensed User
Longtime User
First of all, thanks for your quick answer.

In this case, I want to ovewrite the database without mantain any change in the old db. (I can do it due the functional characteristics of the app).

I generate the apk of the version 1.5, but when I install it to upgrade from 1.4, the code is correctly upgrade to the version 1.5, but the database is still the database from 1.4 version :-(

In my code, I only have this piece related to DB existing validation:

If File.Exists(File.DirDefaultExternal,"Application.sql")=False Then
File.Copy(File.DirAssets,"Application.sql",File.DirDefaultExternal,"Application.sql")
End If


My target is to replace the old database (1.4) to newer (1.5) when I install/upgrade the application.

Any suggestion/help will be welcome :)
Thanks
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Effectively the existing DB is still open, you need to close it, to pick up the new one.

Is this just for you or all Users? You can either set it to Close the DB, so that it has to be opened, or if just you, go to Setttings, Applications and remove all of the data, which will clear the DB and mean the new one is opened instead.
 
Upvote 0

Melchor99

Member
Licensed User
Longtime User
In this case is for all users.

Sorry, I have a doubt: Changing, Settings-Applications-Remove all of the data, will remove the db file during the apk installation?

This should be a solution, the target is to have the db file of the v1.5 instead of 1.4 once the apk installation upgrade from 1.4 to 1.5 finishes

Thanks!
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Another solution would be to change the name of the DB, if you have this code:
B4X:
If File.Exists(File.DirDefaultExternal,"Application.sql")=False Then 
File.Copy(File.DirAssets,"Application.sql",File.DirDefaultExternal,"Application.sql")
End If
That will PREVENT to copy the new DB because ALREADY EXISTS, so to make things simpler for the user, change the DB name to "Application15.sql" for example, that of course means you have to modify your code but it will be very simple to manage.

You cannot ask a user to do "extra" stuff, that should be the responsibility of the developer.
 
Upvote 0

Melchor99

Member
Licensed User
Longtime User
Thanks, I think changing the name of the database is the best way to solve the problem. The old database will continue in the installation path after the apk upgrade but it's no too much heavy so I think it's not a big problem.

Sorry, my english is not to well, I didn't want to ask for extra stuff just only for an idea but reading my previous mail I understand that it seems a stuff request. Sorry for this.

Thanks again!

Regards
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hello,

This is my solution, using a column "Versie" in a table:

B4X:
    If FirstTime = True Then
        If File.Exists(File.DirDefaultExternal, "db.sql") = False Then
            File.Copy(File.DirAssets,"db.sql", File.DirDefaultExternal,"db.sql")   
            ST.Initialize(File.DirDefaultExternal, "db.sql", False)
            StartActivity(..........)
        Else If File.Exists(File.DirDefaultExternal, "db.sql") = True Then
            File.Copy(File.DirAssets,"db.sql", File.DirRootExternal,"db2.sql")
            ST.Initialize(File.DirRootExternal, "db2.sql", False)
            STNew.Initialize(File.DirRootExternal, "db2.sql", False)
        ST.Initialize(File.DirDefaultExternal, "db.sql", False)           
            crVersie = ST.ExecQuery("Select Versie FROM table_name")
            crVersie.Position = 0
            VersieOld = crVersie.GetString("Versie")
            crVersie = STNew.ExecQuery("Select Versie FROM table_name")
            crVersie.Position = 0
            VersieNew = crVersie.GetString("Versie")
            crVersie.Close

            If VersieNew <> VersieOld OR VersieOld = "" Then
                ST.Close
                STNew.Close
                File.Delete(File.DirDefaultExternal, "db.sql")
                File.Copy(File.DirRootExternal, "db2.sql", File.DirDefaultExternal, "db.sql")
                File.Delete(File.DirRootExternal, "db2.sql")
                ST.Initialize(File.DirDefaultExternal, "db.sql", False)
                StartActivity(..................)
               
                File.Delete(File.DirRootExternal, "db2.sql")
            End If
        End If
        File.Delete(File.DirRootExternal, "db2.sql")
        File.Delete(File.DirRootExternal, "Temp.apk")
End If
 
Upvote 0
Top