是否可以将 sqlite DB 的默认路径更改为应用程序的应用程序缓存目录。我有一个显示 web 视图的应用程序。我无法判断页面是否在缓存中,所以我已经将页面路径写入 sqlite db。如果用户删除缓存目录,那么我也希望删除数据库。
这可能吗?
提前致谢
[编辑]
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;
public class BmiDB {
private static final String TAG = BmiDB.class.getSimpleName();
//table websitecache column names
public static final String C_ID_WEBSITE_CACHE = BaseColumns._ID;
public static final String C_PATH_FROM_ROOT = "pathfromroot";
public static final String C_VERSION = "version";
Context context;
DBHelper dbhelper;
ApplicationExt appObj;
public BmiDB(Context context) {
this.context = context;
dbhelper = new DBHelper();
appObj = (ApplicationExt)context.getApplicationContext();
}
public void deleteTableWebsiteCache() {
// open database
SQLiteDatabase db = dbhelper.getWritableDatabase();
// delete contents of table
db.delete(DBHelper.TABLEWEBSITECACHE, null, null);
// close database
db.close();
}
/**
* inner class to create/open/upgrade database
*
* @author matt
*
*/
private class DBHelper extends SQLiteOpenHelper {
// database name and version number
public static final String DB_NAME = "bmi.db";
public static final int DB_VERSION = 1;
//table names
public static final String TABLEWEBSITECACHE = "websitecache";
public DBHelper() {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sqlToCreateWebsiteCache = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT)",
TABLEWEBSITECACHE, C_ID_WEBSITE_CACHE, C_PATH_FROM_ROOT, C_VERSION );
db.execSQL(sqlToCreateWebsiteCache);
Log.e(TAG, "oncreate " + sqlToCreateWebsiteCache);
}//end of onCreate
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLEWEBSITECACHE);
Log.e(TAG, "onUpgrade dropped table");
this.onCreate(db);
}//end of onUpGrade
}//end of DBHelper
public void close() {
dbhelper.close();
}
public void insertIntoWebsiteCache(ContentValues values) {
SQLiteDatabase db = dbhelper.getWritableDatabase();
db.insertWithOnConflict(DBHelper.TABLEWEBSITECACHE, null, values,
SQLiteDatabase.CONFLICT_REPLACE);
db.close();
}
public Cursor queryAllFromWebsiteCacheTable() {
// open database
SQLiteDatabase db = dbhelper.getReadableDatabase();
return db.query(DBHelper.TABLEWEBSITECACHE, null, null, null, null, null,null);
}
}//end of class
.