Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android removing a SQL database

I have a strange problem where my SQL database doesn't remove itself when I uninstall the app

I'm not creating the database in any external storage directories, and the database is confirmed to be in its default location: /data/data/com.example.app/databases/

After I uninstall the app, I expect all the data in that location to be wiped, but when I run the app again from android studio the new install can still see all of the old data

I have tried using adb to manually delete the .db file from the databases directory, but when I run the app again the old data can still be seen

I have also tried going into the app settings and clearing the data and cache, but that doesn't seem to help

The only places in my code where I'm doing anything "strange" with regards to the database is I'm copying the database file to a new location on button click:

String pathToExternalStorage = Environment.getExternalStorageDirectory().toString();
File exportDir = new File(pathToExternalStorage, "/SensorData");
File subjectDataDir = new File(exportDir, "/subjects");

File data = Environment.getDataDirectory();
String currentDBPath = "//data//com.example.app//databases//" + DBHelper.DATABASE_NAME;
File currentDB = new File(data, currentDBPath);
File destDB = new File(exportDir, DBHelper.DATABASE_NAME);

FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(destDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();

And then running mediascanner on the newly created file so I can get instant MTP access to it

I doubt that either of these processes could cause this to happen though?

My immediate question is how do I manually remove the database from the phone without root access?

The bigger question is what could be causing the uninstall process to not remove the database files? And how does a newly installed version of the app (after an uninstall) still see the old database information?

I'm not sure how to begin figuring this out...

EDIT:

This is getting stranger. This only seems to happen on Windows. This is the process I tried:

  1. Delete/uninstall the app from the phone
  2. Run app from android studio on Windows. Problem: database still exists and app sees all old data (even though I uninstalled the app)
  3. Uninstall app
  4. Run app from android studio on my macbook. No problem! The database is fresh, and no existing data is found
  5. Uninstall app again
  6. Test again on my Windows machine. Now, the database comes back and the app sees the old old database info (from step 2!). Uninstall at this point doesn't get rid of it

So I really am at a loss now. Why would running on a Mac be OK? And why would, after running on a Mac and cleaning out the database, then bring the old Windows database again? Should this not all be OS independent?

like image 846
Simon Avatar asked Oct 30 '22 06:10

Simon


1 Answers

I know this is very old, but I was having the same issue and turning auto backup off fixed it (the database was being saved to the cloud and restored).

Go into your manifest, and under application add

android:allowBackup="false"

Hope this helps anyone having this issue.

like image 55
CsSingleton Avatar answered Nov 11 '22 12:11

CsSingleton