I have uploaded an apk (version 1.0) with 22 tables in SQLite Database on Google Playstore.
Now i want to update database with 36 tables in new version (version 2.0) of application.
I am storing datebase at default location so when i press "Clear
data" in Application Manager, database is gonna deleted.
I just want to know how to delete old database (same as clear data) when user update new version?
Update:
If is there any solution for clear data while updating application from play store then also answered me.
Your help would be appreciated.
Thanks.
you can use this method to delete your database.
context.deleteDatabase(DATABASE_NAME);
You can also use this method to find your database path first and then delete that.
File myDb = context.getDatabasePath(DATABSE_NAME);
Boolean isDelete = myDb.delete();
The other solution is , if you want to update your database then just change your version number of database. onUpgrade() will automatically get called and your old database will be deleted and new database will be created.
Finally done with simple solution:
/** UPGRADE DATABASE **/
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i(TAG, "Database Version: OLD: "+ oldVersion + " = NEW: "+newVersion);
if(context.deleteDatabase(DATABASE_NAME))
Log.i(TAG, "Database Deleted....");
else
Log.i(TAG, "Database Not Deleted..");
}
Nice Question. Just follow the steps.
1) There are two methods that you override on Your Helper class
#Override
public void onCreate(SQLiteDatabase db) {
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion!=newVersion){
// Remove all old tables and crate new ones
}
}
2) create an object of Helper class.
MyOpenHelper dbHelper = new MyOpenHelper(this,"dbname", 1);
3) Now just increase the database version when you want to change the database.(Third argument in MyOpenHelper class. "1" is a database version).
4) When database version changes it will give you a callback in onUograde() method.
5) Remove all tables in onUpgrade() method and create new ones.
6) That's it.
EXPLANATION :
Your app version 1.1 is on google play and your database version is "1". Now you want to upload version 1.2 with new database. Just set database version "2" in your helper file. And check the the oldVersion and newVersion in onUpgrade() method if both are not same that means application updating and remove old tables and create new ones.
If you are using SQLiteOpenHelper just delete your tables in your onUpgrade method and recreate all your tables again. You should have not problems at all.
Check this out.
Hope it helps
Note: Be careful with database versions, if you are playing with your database numbers make sure you have the right number as your oldVersion or it will not work properly when you update your google play.
The obvious solution is to delete the old DB file when onUpgrade() is called and copy the new version over. Too bad it does not work. You get an error something like this:
Caused by: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
SQLiteLog﹕ (1032) statement aborts at 4: [PRAGMA user_version = 2]
SQLiteLog﹕ (28) file unlinked while open: /data/data/my.package/databases/my_db.db
You don't need to delete the database, just copy over it using the method you've alady defined (copyDataBase), like this:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Toast.makeText(myContext, "onUpgrade called!", Toast.LENGTH_LONG).show();
if (oldVersion < newVersion) {
Log.v("Database Upgrade", "Database version higher, upgrading");
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error upgrading database");
}
}
}
wrok for me, thank
Related
i have a android app of version 4.20 inside 10M sqlitedb, now i have a app of version 4.21 inside 100M sqlitedb,i want upgrade 4.20 to 4.21, when it upgrade have completed, i find the sqlite size still keep 10M in 4.21 app.
so the question is how to override sqlitedb when upgrade app 4.20 -> 4.21 。
My business is: app will have a lot of data synchronization. In order to optimize, every app will fill in initialization data into SQLite when it is published. Therefore, after each app have upgraded, I hope to completely overwrite SQLite。
any help is very apprecated.
You can upgrade your database version and in the onUpgrade() method of your SQLiteOpenHelper drop all tables and call onCreate() method for recreat database.
public class MyOpenHelper extends SQLiteOpenHelper {
...
private void resetDB(SQLiteDatabase db) {
// Drop all tables
db.execSQL("DROP TABLE IF EXISTS table_name");
// TODO duplicate above line for other tables
// Crate tables again
onCreate(db);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
resetDB(db);
}
#Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onDowngrade(db, oldVersion, newVersion);
resetDB(db);
}
}
The Problem is the onUpgrade function is not being called because when you load a database from assets the the default version is 0.
And when you call the db.getReadableDatabase() or db.getWriteableDatabase which is supposed to call the onUpgrade method, it fails because the version number 0 which is supposed to be a fresh database.
If you see the SqliteOpenHelper source code
db.beginTransaction();
try {
//Skips updating in your case
if (version == 0) {
onCreate(db);
} else {
if (version > mNewVersion) {
onDowngrade(db, version, mNewVersion);
} else {
onUpgrade(db, version, mNewVersion);
}
}
db.setVersion(mNewVersion);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
When version number of db is is 0 the onUpgrade function is not called.
One way to fix the problem is change the version number of db when you add it to the asset or change it before opening
The solution change the version number and your problem
try
{
String myPath = MyApplication.context.getDatabasePath(DATABASE_NAME).toString();
//open a database directly without Sqliteopenhelper
SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
//if the version is default 0 the update the version
if (myDataBase.getVersion() == 0)
{
//update the database version to the previous one
myDataBase.execSQL("PRAGMA user_version = " + 1);
}
//Close DataBase
myDataBase.close();
}
catch (Exception e)
{
//Do Nothing a fresh install happened
}
I want to add new columns to a SQLite database, but I already released my app on the Play Store so, if I edit it, users need to uninstall and reinstall the app,
but I don't want that. Please, help, I am new to Android.
(1) Increment (or simply change) your database version
(2) It would lead to onUpgrade() method call
(3) Execute your query(for adding a new column) in the onUpgrade() method.
The Right Way of doing is mentioned here in this blog.
Sometimes user may update the version 1.5 from the version 1.1.In other words, They may skip the other versions between the 1.1 to 1.5. You might changed database couple of time between 1.1 to 1.5. So in order to give the user a benefit of all the database changes, you need to take of onUpgrade() method as below.
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
db.execSQL(DATABASE_ALTER_TEAM_1);
}
if (oldVersion < 3) {
db.execSQL(DATABASE_ALTER_TEAM_2);
}
}
Hope it helps.
You need to changes in following method of Database Class
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "ALTER TABLE " + TABLE_SECRET + " ADD COLUMN " +
"name_of_column_to_be_added" + " INTEGER";
db.execSQL(sql);
}
Is it possible to delete any existing data set by a previous install of same app (databse tables and shared preferences etc) when the app is re-installed?
I have an app that stores some values in sqlite database, if the app is re-installed without prior properly uninstalling. I face problems from previous database entries etc.
If uninstalling the app didn't do the stuff try this :
System Parameters -> Manage Applications -> Your Application -> Clear data
If you click on the button (Clear data) you will see a dialog which shows you what kind of data will be cleared.
Edit:
If you want to do that programmatically, you can :
Change database version in the super method of the constructor:
super(context, DATABASE_NAME, null, NEW_DB_VERSION);
Add a drop statement before creating tables.
database.execSQL("DROP TABLE IF EXISTS your_table");
database.execSQL(" CREATE TABLE your_table ...");
Proceed to a hard drop of the database:
this.context.deleteDatabase(YOUR_DATABASE_NAME;
Its very Simple.
First Delete the table using drop query
sdb.execSQL("DROP TABLE IF EXISTS tablename");
and then again use the create table query
sdb.execSQL(" CREATE TABLE tablename(col1 TEXT PRIMARY KEY)");
or
delete the DB file using file explorer in path data->data->package->databases->dbname
update the Database version to greater value in the OpenHelper, it will automatically drop all the tables in database and recreate them.
For shared preferences.. you can clear them from the OpenHelper when onUpgrade is called.
Like Ankit Popli said, Using version is the right way to go:
public class Database extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "YourDBName.db";
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// ALl the deletion operation here.
db.execSQL("DROP TABLE IF EXISTS "+Database.TABLE);
onCreate(db);
}
}
AndroidSqlite will automatically call onUpgrade function when the version number is incremented. Do all the database deletion there.
I add one more table to my app's database.
However, if I upgrade my app without clearing its data, the new table doesn't seem to be added automatically.
Therefore, some code that need to retrieve data from the new table fail.
Is it possible to add new tables automatically on app upgrade?
Please implement onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) method.
There is an Android helper class to manage database creation and version management using an application's raw asset files.
Have a look at this :
https://github.com/jgilfelt/android-sqlite-asset-helper
Yes there is a method onUpgrade
add you code there and increase database version if your version is higher than the previous
onUpgrade is fired and the your code will execute which you write to add table
sample
Remember increasing you version code in super call
class Database extends SQLiteOpenHelper
{
public Database(Context _myContext)
{
super(_myContext, DB_NAME, null, DB_VERSION_CODE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if(oldVersion > newVersion)
{
// create a new table what you want to create you can also check the existence of the table
}
}
}
Typically, for a WinForm or a Web App, I create the database and tables through the RDBMS or through a separate install process. However, I haven't seen anything of the sort in Android. All the examples I've seen have the database creation scripts embedded in an activity like this.
The best thing I can come up with now is to call a method from the data access constructor to check whether the database is installed - if not - install it. However, this seems like a lot of overhead to me.
What's the cleanest way to execute a android database install and then forget about it?
When using SQLLiteOpenHelper (http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html), the onCreate method will be called only if the database doesn't exist. onUpgrade will be called when a new version of the database is introduced.
IF the database already exists, and no version upgrade occured, these methods won't be executed.
There is no need for implementing if-else checks in your activity.
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion);
db.execSQL(DATABASE_UPGRADE);
}
}