将sqlite数据库路径更改为应用程序缓存目录

时间:2013-02-06 11:51:56

标签: android sqlite

是否可以将sqlite数据库的默认路径更改为应用程序的应用程序缓存目录。我有一个显示webview的应用程序。我无法判断页面是否在缓存中,所以我已经将页面路径写入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

1 个答案:

答案 0 :(得分:2)

目前还不清楚files/documents/users/servername/data是否与您的应用程序或根文件系统相关。

经验法则:不要使用指向/来自根文件系统的绝对路径名。永远。事情会破裂并最终陷入愚蠢的错误!

要获取应用程序的根目录,请使用:

String ROOT_DIR = context.getFilesDir().getAbsolutePath();

从那里,将数据库移动到您想要的任何位置(在应用程序的存储树下)。