Android SQLite database upgrade, get previous version - android

Is there a way, when there is a database upgrade with onUpgrade() method, to get the previous database?.
I mean, if current database version in 2 and I change it to 3, onUpgrade method is called, but after that is there a way to get the database corresponding to version 2 ?

No, there isn't. When you upgrade your database, the older version is overwritten.
You could try making a backup copy by copying the file from the internal storage's databases folder to your normal internal storage or the external storage, and then restoring it if you have to.

You cannot get the previous version, because now it is upgraded to version 3.
What you can do, is downgrade the database to the previous version with onDowngrade when you deliver a new app with database version 2. Although, this is only available from API level 11.

Related

Upgrading database Sugar ORM always delete previous table's data

I have a database with Sugar ORM, and every time I create a new class that extends SugarRecord, I need to upgrade my database_version on android manifest to Sugar ORM recognize the new table. Ok till then.
The problem is that Sugar ORM is deleting all my previous data on every table of my database! I have a lot of data on them, and I can't delete it and reinsert it on every database upgrade.
There is some way to avoid this?
Thanks
Create a folder named sugar_upgrades in your assets folder.
Create a file named <version>.sql in that folder, which corresponds to the database version you are upgrading to in Android Manifest.
eg. 1.sql, 2.sql This file would contain all the update/alter queries for that particular version.
Change the VERSION metadata field in AndroidManifest.xml to the appropriate version.
Sugar takes care of upgrading the database from its present version to the upgraded version. For eg: if the database is currently at version 1 and the upgraded version is 4, it'd look for 2.sql, 3.sql and 4.sql and execute all of the present files in that order.
Note that Sugar will automatically create tables for new entities, so your migration script only needs to cater for alterations to existing tables.
Upgrade Script sample.
This is a normal sql script file. You can add all your alter and insert/update queries, one line at a time. Each line is terminated by a ; (semicolon).
file in your assets/sugar_upgrades folder : 2.sql
alter table NOTE add NAME TEXT;
you can just use sugar orm 1.5 version like
compile 'com.github.satyan:sugar:1.5'
and then just update database version in manifest and be sure that your data doesn't wiped

SQLiteOpenHelper.onUpgrade how does it exactly works? (app with multiple databases)

Google documentation for SQLiteOpenHelper.onUpgrade is very general. I'm wondering how onUpgrade execution exactly works?
Does Android stores database version number inside database itself (I tried to find it using database tool aSQLiteManager, btw very good piece of software, but it seems to be hidden if exist)? Or does Android just compares the old and new version of app in the moment of installation and basing on this it definieds old and new version as parameters of onUpgrade.
Let's take an example. I have app verson 1 which can work with multiple databases. At the beginning I have only database A version 1. Next I upgrade app to version 2 and database A is also upgraded to version 2 (onUpgrade is executed). Next I get database B from my friend created by the same app but in version 1 (thus B has also version 1). I copy it to the appropriate dir and run my app. Will onUpgrade method be executed then (for database B version 1, no app reinstallation)?
If you want, you may want to use SQLiteDabase (the first parameter of onUpgrade), with the method getPath to know which databse will be updated.
When you will ask SQLiteOpenHelper.getWritableDatabase (or getReadableDatabase()), it will be updated if the version you set in constructor is greater than the current version of the database.
If you need different upgrade statements for different databases (which is generally the case), you may want to declare two classes extending SQLiteOpenHelper.

Issue related to upgrading application related to Database in Android

When i modify the database and upgrade the existing app in my Phone, the DB is not getting overwritten which makes my application crash.
How to tell phone to delete the DB and add fresh one during installation of APK file?
Did you try to increase the version number you pass to the the SQLiteOpenHelper constructor.
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

Android Internal Storage when updating application

I use the internal storage (http://developer.android.com/guide/topics/data/data-storage.html#filesInternal) to store some necessary application files, that the user creates.
I suppose that these files are stored forever right? They are removed only when the user uninstalls the application, am I correct?
In case of updating the application (for example from version 1.0 to 1.1) through the Android Market, then what will happen to the files that were stored from the previous version? Are they deleted or not?
Thank you in advance.
In case of updating the application (for example from version 1.0 to 1.1) through the Android Market, the files that were stored from the previous version will be there on the device and if you uninstall the app to get the new version then the files will be deleted.
Good question - the docs actually don't say anything about any of the persistence solutions retaining their data on upgrade. But just as SharedPreferences and Sqlite database, I suppose it would also persist during application upgrade. The best way to be sure is to test it yourself, though.
(I know it may sound rude, as I'm like telling you to "see for yourself". But I won't mind doing it for you if my current machine is set up for android development)

Force SQLite 3.7 to run in legacy mode?

My Android app works with SQLite databases. They are created in SQLite v3.6 (legacy mode) so that they are compatible with all Android devices. However, a small set of the devices running SQLite v3.7 are auto-promoting the SQLite db's to v3.7, which makes them incompatible with older devices.
Is there a way to tell devices running v3.7 not to upgrade the database?
PRAGMA legacy_file_format = 1
According to the SQLite website:
This pragma sets or queries the value of the legacy_file_format flag.
When this flag is on, new SQLite databases are created in a file
format that is readable and writable by all versions of SQLite going
back to 3.0.0. When the flag is off, new databases are created using
the latest file format which might not be readable or writable by
versions of SQLite prior to 3.3.0.

Categories

Resources