私のAndroidアプリでは、データの保存と取得にSQLiteデータベースを使用しています。
ユーザーがアプリケーションマネージャーからデータをクリアすると、データはクリアされます.しかし、私の要件は、ユーザーがアプリケーションマネージャーからデータをクリアすると、データが静的になり、削除されないことです。
そのデータベースをどのように管理できますか?
解決策を教えてください。
前もって感謝します..
ユーザーは必要に応じてデータを消去できます。したがって、データベースを静的にすることはできません。ただし、データベースを保持したい場合は、データベースをassetsフォルダーにコピーでき、アプリonCreate
がSQLiteHelperのメソッドを呼び出すときに、assetsフォルダーからデバイスにコピーします。
サンプルは次のとおりです。
DBヘルパー
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;
}
}