SQLite database status on app uninstall

删除回忆录丶 提交于 2019-12-11 06:06:55

问题


I have a few doubts regarding SQLite DB status on uninstall of an Android application.

  1. What happens to SQLite DB when an app is uninstalled?
  2. How to seamlessly save SQlite DB on uninstalling of app if my device doesn't have an external store(SD card).
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!