Android 将sqlite db路径更改为应用程序缓存目录
是否可以将sqlite DB的默认路径更改为应用程序的应用程序缓存目录。我有一个显示网络视图的应用程序。我无法判断页面是否在缓存中,因此我将页面路径写入了sqlite db。如果用户删除了缓存目录,那么我希望数据库也被删除 这可能吗 提前谢谢 [编辑]Android 将sqlite db路径更改为应用程序缓存目录,android,sqlite,Android,Sqlite,是否可以将sqlite DB的默认路径更改为应用程序的应用程序缓存目录。我有一个显示网络视图的应用程序。我无法判断页面是否在缓存中,因此我将页面路径写入了sqlite db。如果用户删除了缓存目录,那么我希望数据库也被删除 这可能吗 提前谢谢 [编辑] import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.databa
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
.不清楚
文件/文档/用户/服务器名/数据是否与您的应用程序或根文件系统相关
经验法则:不要使用指向根文件系统的绝对路径名。曾经事情会破裂,最终变成愚蠢的虫子
要获取应用程序的根目录,请使用以下命令:
String ROOT_DIR = context.getFilesDir().getAbsolutePath();
从那里,将数据库移动到您想要的任何位置(在应用程序的存储树下)。您好,我已经更新了帖子,加入了我的DB类和助手。很抱歉,我是新手,我可以指定在这个类中从何处写入数据库吗。如果不是的话,请你详细说明我是如何做到的。再次感谢