0

My sqlite database is automatically deleted when my app is uninstalled. I wish to keep the database, so I don't need to rebuild the database when I reinstall the app.

tata
  • 21
  • 3
  • Make backup and restore database or synchronize your database with an API so each time you have the same content restored – Maytham Fahmi May 28 '17 at 04:03
  • Possible duplicate of [How to store sqlite database directly on sdcard](https://stackoverflow.com/questions/14373863/how-to-store-sqlite-database-directly-on-sdcard) – Rishabh Maurya May 28 '17 at 04:05
  • @tata It's a design by default, what you need is an ADB command that backups your SQLite into your PC before deleting the app. – Enzokie May 28 '17 at 04:08
  • 1
    Since Android 6.0, apps can use Autobackup to backup their database daily. It will be restored automatically when the app is reinstalled. https://developer.android.com/guide/topics/data/autobackup.html – BladeCoder May 28 '17 at 10:21

2 Answers2

4

By default, all databases of Android app are saved in /data/data/your-package-name/databases folder. When you uninstall the app, the /data/data/your-package-name directory and all of its subdirectory are deleted. You can't keep the database files if they are saved in this location.

But If you save your database files to SD card, the files are kept even after uninstall.

Here is an example of creating database is external directory

public class DatabaseHelper extends SQLiteOpenHelper {
    public DatabaseHelper(final Context context) {
        super(context, Environment.getExternalStorageDirectory()
                + File.separator + FILE_DIR
                + File.separator + DATABASE_NAME, null, DATABASE_VERSION);
    ...

    }
    ...

}
Sourav Bagchi
  • 656
  • 7
  • 13
1

This question is answered here How to store sqlite database directly on sdcard

Remember to add permission to write external storage in your manifest

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

Also I see that READ_EXTERNAL_STORAGE & WRITE_EXTERNAL_STORAGE are classified as 'Dangerous Permissions'

reff: https://developer.android.com/guide/topics/permissions/requesting.html

You'll need to get user permission AT RUNTIME (only once). This page describes how to do it: https://developer.android.com/training/permissions/requesting.html

Here is sample code (from Read and Write permission for storage and gallery usage for marshmallow) that does this check

public class MainActivity extends AppCompatActivity implements 

ActivityCompat.OnRequestPermissionsResultCallback{

  private static final int REQUEST_WRITE_PERMISSION = 786;

  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {            
        openFilePicker();
    }
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    requestPermission();
  }

  private void requestPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
    } else {
        openFilePicker();
    }
  }
}
CanonicalBear
  • 367
  • 8
  • 12