问题
I have a few doubts regarding SQLite DB status on uninstall of an Android application.
- What happens to SQLite DB when an app is uninstalled?
- How to seamlessly save SQlite DB on uninstalling of app if my device doesn't have an external store(SD card).
- recommended way to store information in DB / encrypt the DB such that it cant be accessed if user has root access to device
回答1:
its a code to put database in internal storage
public class DatabaseAdapter extends SQLiteOpenHelper
{
private static String DB_PATH = FileUtil.CreateDirByName("database")+"/";
private static String DB_NAME = "YourDBName.sqlite";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase myDataBase;
private final Context myContext;
public DatabaseAdapter(Context context)
{
super(context, DB_NAME, null, DATABASE_VERSION);
this.myContext = context;
try
{
createDataBase();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist)
{
//do nothing - database already exist
}
else
{
super.getWritableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
// TODO Auto-generated method stub
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}catch(SQLiteException e){
e.printStackTrace();
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
@Override
public SQLiteDatabase getReadableDatabase(){
try{
if(myDataBase != null)
myDataBase.close();
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}catch(SQLiteException e){
e.printStackTrace();
}
return myDataBase;
}
@Override
public SQLiteDatabase getWritableDatabase(){
try{
if(myDataBase != null)
myDataBase.close();
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}catch(SQLiteException e){
e.printStackTrace();
}
return myDataBase;
}
private void copyDataBase() throws IOException{
//Read the DB
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException
{
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
}
回答2:
What happens to SQLite DB when an app is uninstalled?
The same thing as happens to any other sort of file. If it is on internal storage, or it is on app-specific locations on external storage (e.g., getExteranlFilesDir()), the database is deleted.
How to seamlessly save SQlite DB on uninstalling of app if my device doesn't have an external store(SD card).
That is not possible. Fortunately, your app does not get control when your app is being uninstalled.
recommended way to store information in DB / encrypt the DB such that it cant be accessed if user has root access to device
Do not put the data on the device.
来源:https://stackoverflow.com/questions/44184575/sqlite-database-status-on-app-uninstall