I have made the android app which work in offline mode and i dont want to lost data after unistallation of app.enter code here

- 11
- 6
-
to fullfill your requirment you should use online data base – Vinesh Chauhan Dec 21 '18 at 04:58
-
I found this [information](https://developer.android.com/guide/topics/data/autobackup) about autobackup of the app. May be it will be helpful – Asset Bekbossynov Dec 21 '18 at 05:00
-
@VineshChauhan:::no, my app is for remote place so it is difficult for me to access internet anytime.thank you – Sanju Dec 21 '18 at 05:01
4 Answers
You could place the database in External Storage
- note not 100% safe you should read External storage.
You will also need appropriate permission to use External Storage
- note the code assumes that for API 23+ that the user gives permission.
Here's a simple working example that stores the database named mydb.db in external storage in a folder named mydatabases, it saves a few rows in the table named mytable. If the App is uninstalled the data remains.
- (NOTE the user would probably be able to delete the database).
First a class to handle requesting permission on API 23+ devices, ExternalStoragePermissionsRequest.java
class ExternalStoragePermissionsRequest {
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
public ExternalStoragePermissionsRequest() {}
// Note call this method
public static void verifyStoragePermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(
activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
}
The Database Helper, DatabaseHelper.java :-
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb.db";
public static final int DBVERSION = 1;
public static final String TBL_MYTABLE = "mytable";
public static final String COL_MYTABLE_ID = BaseColumns._ID;
public static final String COL_MYTABLE_MYDATA = "mydata";
SQLiteDatabase mDB;
public DatabaseHelper(Context context) {
super(context,
Environment.getExternalStorageDirectory() + File.separator + "mydatabases" + File.separatorChar + DBNAME,
null,
DBVERSION
);
mDB = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String crt_sql = "CREATE TABLE IF NOT EXISTS " + TBL_MYTABLE + "(" +
COL_MYTABLE_ID + " INTEGER PRIMARY KEY," +
COL_MYTABLE_MYDATA + " TEXT " +
")";
db.execSQL(crt_sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long insert(String mydata) {
ContentValues cv = new ContentValues();
cv.put(COL_MYTABLE_MYDATA,mydata);
return mDB.insert(TBL_MYTABLE,null,cv);
}
public void logData() {
Cursor csr = mDB.query(TBL_MYTABLE,null,null,null,null,null,null);
DatabaseUtils.dumpCursor(csr);
csr.close();
}
}
- Note the 2nd parameter of the call to the super method.
Finally an invoking activity (assumed the first/initial/main) MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseHelper mDBHlpr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(Build.VERSION.SDK_INT >= 23) {
ExternalStoragePermissionsRequest.verifyStoragePermissions(this);
}
mDBHlpr = new DatabaseHelper(this);
mDBHlpr.logData();
if (DatabaseUtils.queryNumEntries(mDBHlpr.mDB,DatabaseHelper.TBL_MYTABLE) < 1) {
mDBHlpr.insert("Somedata001");
mDBHlpr.insert("Somedata002");
}
mDBHlpr.logData();
}
}
- Note to ideally check that the above is uninstall safe (disclaimer re using External Storage), the 2 lines that insert the data should be commented out.
The log should when first run and when run after uninstalling the app and commenting out the lines as suggested, should include :-
12-21 06:47:55.306 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@53442310
12-21 06:47:55.306 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: 0 {
12-21 06:47:55.306 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: _id=1
12-21 06:47:55.306 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: mydata=Somedata001
12-21 06:47:55.306 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: }
12-21 06:47:55.306 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: 1 {
12-21 06:47:55.306 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: _id=2
12-21 06:47:55.306 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: mydata=Somedata002
12-21 06:47:55.306 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: }
12-21 06:47:55.306 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: <<<<<
12-21 06:47:55.306 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5343da94
12-21 06:47:55.310 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: 0 {
12-21 06:47:55.310 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: _id=1
12-21 06:47:55.310 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: mydata=Somedata001
12-21 06:47:55.310 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: }
12-21 06:47:55.310 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: 1 {
12-21 06:47:55.310 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: _id=2
12-21 06:47:55.310 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: mydata=Somedata002
12-21 06:47:55.310 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: }
12-21 06:47:55.310 2671-2671/so53879343esdb.so53879343externalstoragedb I/System.out: <<<<<

- 51,415
- 16
- 49
- 68
-
Mike::i implemented ur solution and it solved my problem but in the beginning i am geting crash so em trying to find that solution..after i implemeted without error i will let u know. – Sanju Dec 21 '18 at 08:40
-
just create your database in external storage like below
public DatabaseHelper(final Context context) {
super(context, Environment.getExternalStorageDirectory()
+ File.separator + FILE_DIR
+ File.separator + DATABASE_NAME, null, DATABASE_VERSION);
}
It will not delete even if you uninstall your app

- 5,601
- 2
- 15
- 27
-
Can you be more specific with your question? I am not getting which link you want. If you could be more specific about it, I would be pleased to help you with it. @Sanju – Mittal Varsani Dec 21 '18 at 05:22
Following steps is helpful to you:
-You can create external SQLITE database and put into our assets folder.
-you can fetch data from database while app install first time.
If any query feel free to ask.

- 10,966
- 5
- 25
- 51
-
This would not be any use if data were added to the database after installation, as any such data would be lost. – MikeT Dec 21 '18 at 05:01
-
I am not saying data added after installation.Please read answer carefully. – dipali Dec 21 '18 at 05:02
-
i dont want to lost my sqlite database after uninstallation of app as my app is fully offline so it contains many important data. – Sanju Dec 21 '18 at 05:13
-
So where does the data come from? If you carefully considered the situation if the poster wasn't aware of copying a pre-existing database then they would be creating a database and populating it and thus in either case your answer serves no useful purpose. – MikeT Dec 21 '18 at 05:15
-
@Sanju Please create one Sqlite database using external sqlite tools, and put this database in our code.so they haven't loss any data at any of the case.If you don't understand how to add sqlite file in our code, then i will help you. – dipali Dec 21 '18 at 05:19
-
@Sanju I suggest you very carefully consider the implications suggested by this answer, as following this answer would not allow any used submitted/collected data to be saved upon uninstallation. It purely allows only pre-defined data to be restored after re-installation and would be useless for the vast majority of useful Apps where the data is different on a per user basis. – MikeT Dec 21 '18 at 05:27
-
@dipali i have created sqlite databse and it works perfectly in both offline and online mode..this app works in remote area so if app is hang then user may uiinstall the app so in that condition i dont want to loss the data of othat user.give me some idea – Sanju Dec 21 '18 at 05:33
-
@MikeT::data comes from api in the beginning..but user fill the form in remote area and save all data in sqlite database and when they get internet then they send that data to server through api – Sanju Dec 21 '18 at 05:35
-
@Sanju as I suggested this answer would be useless for your situation, so having the database in External Storage is the path that you want. perhaps have a look at [Data and file storage overview](https://developer.android.com/guide/topics/data/data-storage) and also perhaps [Save files on device storage](https://developer.android.com/training/data-storage/files#WriteExternalStorage) – MikeT Dec 21 '18 at 05:57
SQlite databases are just files, which are stored (by default) in the application's private data area (/data/data/$PACKAGENAME/databases). These files are removed when your application is uninstalled. Fix for your solution is to create your db on SDCard.
Take a look on this

- 46
- 2
- 6