【发布时间】:2017-10-26 08:15:40
【问题描述】:
我对卸载 Android 应用程序时的 SQLite DB 状态有一些疑问。
- 卸载应用后,SQLite DB 会发生什么情况?
- 如果我的设备没有外部存储(SD 卡),如何在卸载应用程序时无缝保存 SQlite DB。
- 推荐的在数据库中存储信息/加密数据库的方法,如果用户对设备具有 root 访问权限,则无法访问它
【问题讨论】:
标签: android sqlite encryption android-sqlite
我对卸载 Android 应用程序时的 SQLite DB 状态有一些疑问。
【问题讨论】:
标签: android sqlite encryption android-sqlite
卸载应用后 SQLite DB 会发生什么?
与任何其他类型的文件发生的事情相同。如果它位于 internal storage,或者位于 external storage 上的特定应用位置(例如,getExteranlFilesDir()),则删除数据库。
如果我的设备没有外部存储(SD 卡),如何在卸载应用时无缝保存 SQlite DB。
这是不可能的。幸运的是,当您的应用被卸载时,您的应用无法获得控制权。
在数据库中存储信息/加密数据库的推荐方式,如果用户对设备具有 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);
}
}
【讨论】: