1

是否可以将 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

.

4

1 回答 1

2

目前尚不清楚files/documents/users/servername/data是相对于您的应用程序还是根文件系统。

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

要获取应用程序的根目录,请使用以下命令:

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

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

于 2013-02-06T11:56:12.347 回答