应用卸载时的SQLite数据库状态 [英] SQLite database status on app uninstall

查看:103
本文介绍了应用卸载时的SQLite数据库状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在卸载Android应用程序时,我对SQLite DB的状态有一些疑问.

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

  1. 卸载应用程序后,SQLite DB会发生什么?
  2. 如果我的设备没有外部存储(SD卡),如何在卸载应用程序时无缝保存SQlite DB.
  3. 推荐的将信息存储在数据库中/对数据库进行加密的方法,以便如果用户具有对设备的root访问权限就无法访问它

推荐答案

它提供了将数据库放入内部存储的代码

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);
    }

}

这篇关于应用卸载时的SQLite数据库状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆