3

I am working on an sq-lite database where I can find an issue in Samsung device. When I uninstall app android OS keeps database that cause conflicts in database version - previously I used version 2 and now I am using version 1 as a parameter in sqllitehelper constructor. By convention when app is uninstalled Android OS deletes all databases, shared preference and cache files.

I used default location of database store in app folder.

This is the error I get:

Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
Farhan Munir
  • 409
  • 5
  • 23
  • What's the path of the database you pass to the ctor? – laalto Feb 25 '14 at 11:19
  • DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } – Farhan Munir Feb 25 '14 at 11:25
  • You can't downgrade your data base version to lower version i.e 2 to 1.always increase the data base version if you have any changes in db structure – naidu Feb 25 '14 at 11:27
  • i know we cant downgrade database version but i uninstall the app and then downgrade the version then this error occurs – Farhan Munir Feb 25 '14 at 11:28
  • Before uninstalling remove db files and clear data.unless you delete and clear the data android keep db files and shared preferences even after uninstalling app – naidu Feb 25 '14 at 11:31
  • Yes, what's `DATABASE_NAME`? – laalto Feb 25 '14 at 12:00

5 Answers5

0

SQlite databases are just files, and they're treated like any other file: they're stored (by default) in the application's private data area (/data/data/$PACKAGENAME/databases). They're deleted along with everything else in the application's private data area.

You can create a database on the SD card if you like. They, of course, won't be removed on uninstallation.

naidu
  • 350
  • 2
  • 8
0

You can use the following to take a backup of your DB on the SDCard:

public void writeToSDCard() throws IOException {
    File f=new File("/data/data/com.YourPackage/databases/YourDB");
    FileInputStream fis=null;
    FileOutputStream fos=null;
    try{
        fis=new FileInputStream(f);
        fos=new FileOutputStream("/mnt/sdcard/backup.db");
        while(true){
            int i=fis.read();
            if(i!=-1){
                fos.write(i);
            }
            else{
                break;
            }
        }
        fos.flush();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
        try{
            fos.close();
            fis.close();
        }
        catch(IOException ioe){
            System.out.println(ioe);
        }
    }
} 
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Skynet
  • 7,820
  • 5
  • 44
  • 80
0

The Solution is to backup your portable Sqlite file from its directory to the SD-Card directory. You can do this by following the next steps:-

1) Add this permission to your Manifest.XML file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2) Call the following method in your Java class:

public boolean packupDatabaseFile(){
    try {
        File sdCardDir = Environment.getExternalStorageDirectory();
        File dataDir = Environment.getDataDirectory();

        if (sdCard.canWrite()) {
            String dbPath = "//data//{your package name}//databases//{your database name}";
            String backupDBPath = "{database name}";
            File dbFile = new File(dataDir , currentDBPath);
            File backupDbFile = new File(sdCardDir , backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(dbFile ).getChannel();
                FileChannel dst = new FileOutputStream(backupDbFile ).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    return true;//Success
    } catch (Exception e) {
    return false;//Failed to backup
    }
}

With the the help of this Question

Community
  • 1
  • 1
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
0

As you are using a SQLiteOpenHelper http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html so this can detect upgrades and downgrades of the database.

So the methods

onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

and

onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)

can be overridden to delete the old database before the new one is created

Tristan Burnside
  • 2,546
  • 1
  • 16
  • 23
0

I had the same problem on a Samsung S8 - apparently on Samsung Android devices the app memory is not automatically deleted when an app is deleted. Thus the old database still exists when a new app version is installed and there is a conflict if the database schema changes but the version number of the database remains the same or is even reduced.

Solution: To delete the database securely, you have to open the detailed view of the affected app in the app area of the system settings . Here you can explicitly delete the data (and thus the database) and empty the cache. After that you can delete the app. The next time you install the app on your Samsung device, the database will be newly created.