Thank you JohnC, When I update the app, the new database file cannot overwrite the old database file with the same name. it cause the app to crash. If I uninstall the old version first, there is no problem. so I require users to uninstall the old version first.Why do you feel you need to do that?
I ask because that is not the standard method when upgrading an app - your app should be designed to allow a user to install a new version on top of your old version.
And if there is some situation that will cause problems if the new version tries to use data from the older version, then you should make the new version be able to detect if the existing data was from an older version, and handle it accordingly, so that it will be able to properly use any existing data that the old version created.
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Frm_Main")
Dim DBFileDir As String
Dim kvs As KeyValueStore
Dim TargetDir As String
DBFileDir=DBUtils.CopyDBFromAssets("TempData") ' TempData is KeyValueStore file
kvs.Initialize( File.DirInternal, "TempData")
If FirstTime And kvs.Get("VersionNum")<Application.VersionCode Then
If File.Exists(File.DirInternal, "123.db") = True Then
File.Delete(File.DirInternal, "123.db") 'delete old database
End If
DBFileDir=DBUtils.CopyDBFromAssets("123.db") 'release new database
kvs.Put("Ini",Application.VersionCode)
Else
DBFileDir=File.DirInternal
End If
End Sub
Thank you JohnC. I delete old database and then release new database, see #6.OK,
Even though there will be problems if a user installs a newer version of your app on top on an older version, there is nothing that you can do to prevent this from physically happening (if the different versions use the same package name, the newer version will ALWAYS replace the previous version). The best that you can hope for is to post a warning in the play store product description for the app, warning the user that they should uninstall the old version before installing the new version. But again, this is not the recommended/standard way to do app updates, so I would not expect much success using this method.
Also, if the old version of the app allows the user to enter any data or set any preferences, then when the user uninstalls the old version, you are forcing the user to loose all their data and settings for the app - which would not be a good user experience.
What I would do is.....first name the new database (that comes with the new version) to be a different name from the filename of the first (older version) database. This way the older database wont be overwritten when the new version is installed on top of the old version.
Then when your app runs for the first time (after it was installed on top of the old version), have it detect if the newer database file exists, and if it does, then have your app do a one-time import of the data from the newer database into the original/older database, and then simply delete the newer database file when the import is done.
This will allow a user to install the newer version on top of an older version (which then follows standard upgrade procedures) and will prevent any file conflicts.
Thank you Peter Simpson. I delete old database and then release new database, see #6.@watesoft you do not and should not be doing it that way. You should infact be checking for the missing tables and columns and then using SQL statements on-the-fly to update the older database to the newest database that is needed.
You should actually be running your updated app and it should be checking to see if a table or column is available. If a table is not available then you should be running your CREATE TABLE statements. If a column in an already created table is not available then your code should be running your ALTER TABLE statements.
Removing an app just to update a database is not feasible at all. You need to look carefully into what I said above.
Thank you Peter Simpson. I delete old database and then release new database, see #6.
Yes,It is fixed databases without user intervention.Are you saying that your users do not enter any data into the apps database???