我如何维护sq-lite数据。用户从应用程序管理器清除数据时

时间:2013-05-22 10:59:19

标签: android sqlite

在我的Android应用程序中,我使用SQLite数据库来存储和检索数据。

当用户清除应用程序管理器中的数据时,数据将被清除。但我的要求是,当用户从应用程序管理器清除数据时,我的数据将 static ,它不会被删除。

我如何管理该数据库?

请提供任何解决方案。

提前致谢..

3 个答案:

答案 0 :(得分:1)

用户可以根据需要清除数据。因此,您无法使数据库保持静态。 但是,如果要保留数据库,可以将数据库复制到assets文件夹,当app调用SQLiteHelper的方法onCreate时,您将从assets文件夹复制到设备。

以下是样本:

<强> DBHelper

package vn.mve.db;

import java.util.List;

public interface DBHelper<T> {
    boolean insert(T val);
    boolean update(T val);
    boolean delete(T val);
    List<T> getList(int type);
    T getChild(Object val);
}

<强> DBHandler

public class DBHandler extends SQLiteOpenHelper {
    private static final String TAG = DBHandler.class.getSimpleName();
    protected SQLiteDatabase db; 
    private final Context context;  
    private static String PACKAGE_NAME = "";
    private static int DATABASE_VERSION = 1;

    public DBHandler(Context context) {
        super(context, Def.DBNAME, null, DATABASE_VERSION);
        this.context = context; 
        PACKAGE_NAME = this.context.getPackageName();
        Def.FOLDER_DB = "/data/data/" + PACKAGE_NAME + "/databases/";
        Log.d(TAG, Def.FOLDER_DB);
        try {
            this.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            this.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        context.deleteDatabase(Def.DBNAME);
        onCreate(db);
    }

    public void createDataBase() throws IOException{
        // for first database;
        boolean dbExist = checkDataBase();
        if(!dbExist){
            try {
                copyDataBase("db/" + Def.DBNAME);
            } catch (Exception e) {
                Log.e(TAG, "createDatabse -> Copy failed!");
                throw new Error("Error copying database");
            }
        } else {
            open();
            boolean isExist = false;
            Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = 'config'", null);
            if (cursor != null) {
                isExist = true;
                cursor.close();
            } else {
                isExist = false;
            }
            close();
            Log.d(TAG, isExist + "");
            if (!isExist) {
                this.context.deleteDatabase(Def.DBNAME);
                try {
                    Log.d(TAG, "createDatabase when database has existed");
                    copyDataBase(Def.DBNAME);
                } catch (Exception e) {
                    Log.e(TAG, "createDatabse -> Copy failed!");
                    throw new Error("Error copying database");
                }               
            }
        }
    }   
    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase(String DB) {
        //Open your local db as the input stream
        InputStream myInput = null;
        //Open the empty db as the output stream
        OutputStream myOutput = null;
        try {
            myInput = context.getResources().getAssets().open(DB);

            // Path to the just created empty db
            String outFileName = Def.FOLDER_DB + Def.DBNAME; 
            myOutput = new FileOutputStream(outFileName);

            //transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0){
                myOutput.write(buffer, 0, length);
            }
        } catch (FileNotFoundException e) {
            Log.e(TAG, "copyDatabase -> File not found.");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "copyDatabase");
        } finally {
              //Close the streams
            try {
                myOutput.flush();
                myOutput.close();
                myInput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }   
    private boolean checkDataBase(){
        boolean checkDB = false;
        try{
            String myPath = Def.FOLDER_DB + Def.DBNAME;
            File dbFile = new File(myPath); 
            checkDB = dbFile.isFile();
            Log.d(TAG, "checkDatabase: " + String.valueOf(checkDB));
            try {
                File fTmp = new File(Def.FOLDER_DB);
                if (!fTmp.exists()) {
                    fTmp.mkdir();
                }
            } catch (Exception e) {
                Log.e(TAG, "checkDatabase" + e.getMessage());
            }
        }catch(SQLiteException e){}
        return checkDB;
    }
    public void open() {
        try {
            String myPath = Def.FOLDER_DB + Def.DBNAME;
            db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);          
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public synchronized void close() {
            if(db != null)
                db.close();
            super.close();
    }           
    public SQLiteDatabase getSqlDb() {
        return db;
    }
    public void setSqlDb(SQLiteDatabase sqlDb) {
        this.db = sqlDb;
    }    
}

在这里:

public class MVideo extends DBHandler implements DBHelper<Video> {
    public static final String TAG = MVideo.class.getSimpleName();
    public MVideo(Context context) {
        super(context);
    }

    @Override
    public boolean insert(Video val) {
        open();
        ContentValues cValues = new ContentValues();
        cValues.put(Def.Video.ID, val.getId());
        cValues.put(Def.Video.TITLE, val.getTitle());
        cValues.put(Def.Video.THUMBNAIL, val.getThumbnail());
        cValues.put(Def.Video.DESCRIPTION, val.getDescription());
        cValues.put(Def.Video.ENGLISH, val.getEnglish());
        cValues.put(Def.Video.VIETNAMESE, val.getVietnamese());
        cValues.put(Def.Video.ISVIEW, val.getIsView());
        long result = db.insert(Def.Video.NAME, null, cValues);
        close();        
        return result > 0;
    }

    public boolean insertList(List<Video> list) {
        open();
        db.execSQL("BEGIN IMMEDIATE TRANSACTION");
        for (Video v : list) {
            db.execSQL(String.format("INSERT INTO " + Def.Video.NAME + " (\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\") VALUES" + 
                    " (\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\")",
                    Def.Video.ID, Def.Video.TITLE, Def.Video.THUMBNAIL, Def.Video.DESCRIPTION, 
                    Def.Video.ENGLISH, Def.Video.VIETNAMESE, Def.Video.ISVIEW,
                    v.getId(), v.getTitle(), v.getThumbnail(), v.getDescription(), v.getEnglish(), v.getVietnamese(), v.getIsView() + ""));
            Log.d(TAG, "insertList -> " + v.toString());
        }
        db.execSQL("COMMIT TRANSACTION");
        close();        
        return true;
    }

    @Override
    public boolean update(Video val) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean delete(Video val) {
        open();
        db.delete(Def.Video.NAME, Def.Video.ID + "=?", new String[]{val.getId()});
        close();
        return false;
    }

    @Override
    public List<Video> getList(int type) {
        List<Video> list = new ArrayList<Video>();
        open();
        Cursor c = db.rawQuery(Def.Video.GET_ALL, null);
        if (c.moveToFirst()) {
            while (c.moveToNext()) {
                String ID = c.getString(0);
                String title = c.getString(1);
                String thumbnail = c.getString(2);
                String description = c.getString(3);
                String english = c.getString(4);
                String vietnamese = c.getString(5);
                boolean isView = Boolean.parseBoolean(c.getString(6));
                list.add(new Video(ID, title, thumbnail, description, english, vietnamese, isView));
            }
        }
        close();
        return list;
    }

    @Override
    public Video getChild(Object val) {
        open();
        Cursor c = db.query(Def.Video.NAME, new String[]{
                Def.Video.ID, Def.Video.TITLE, Def.Video.THUMBNAIL, Def.Video.DESCRIPTION, 
                Def.Video.ENGLISH, Def.Video.VIETNAMESE, Def.Video.ISVIEW
        }, Def.Video.ID + "=?", new String[]{val.toString()}, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        Video v = new Video(c.getString(0), c.getString(1), 
                c.getString(2), c.getString(3), c.getString(4), 
                c.getString(5), Boolean.parseBoolean(c.getString(6)));
        close();
        return v;
    } 
}

答案 1 :(得分:0)

用户随时可以自由删除应用程序数据,否则任何应用程序都可能填满手机,拒绝卸载并需要恢复出厂设置才能清理。

如果您确实需要安全存储数据,请将其备份到云端,并在用户重新安装应用程序时将其还原。

答案 2 :(得分:0)

数据库是保存数据以供离线使用的地方。

我知道的唯一可能性是在您选择的目录中在SDCard上创建数据库备份。当用户从应用程序管理器清除数据时,不会删除此文件夹。这个“解决方案”的缺点是你需要实现恢复备份的逻辑。此外,您的备份将在任何卸载和新安装后继续存在,我怀疑您是否要从以前的安装中恢复数据库。

简而言之:这可能是一种可能的方式,但它有很多陷阱,并不是真的推荐。让用户保持对应用程序的控制并牢记:用户曾经发现你在SD卡上有备份,他最终也会删除它。