指定数据库存储路径

SQLite创建的数据库默认的存放路径:阅读SQLiteOpenHelper的源码可以发现,创建数据库文件的路径是使用传入的contex的getDatabasePath获取的,这个

是不允许修改的。也就是说SQLiteOpenHelper源码中的存储路径是由方法getDatabasePath()获得的。 接下来就好办了,我们虽然不能够改变这个路径,但是我们可

以覆盖这个方法。

所以如果要创建指定路径的数据库,我们只需要新建一个类MySQLiteOpenHelper,将SQLiteOpenHelper得源码拷贝下来,然后在其中添加我们覆盖的方法

getDatabasePath()。getDatabasePath()的覆盖可以是实例化的覆盖(即在MySQLiteOpenHelper中直接创建方法getDatabasePath())

也可以使抽象的覆盖(只在MySQLiteOpenHelper中声明getDatabasePath()的接口,然后具体的路径就由使用者去确定了))。

最后,当我们我们编写操作数据库的代码时,helper类直接继承我们自定的抽象类MySQLiteOpenHelper就可以了。我的MySQLiteOpenHelper源码如下:

 

import java.io.File;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public abstract class MyDatabaseHelper {
    private static final String TAG = MyDatabaseHelper.class.getSimpleName();
    private final Context mContext;
    private final String mName;
    private final CursorFactory mFactory;
    private final int mNewVersion;
    private SQLiteDatabase mDatabase = null;
    private boolean mIsInitializing = false;

    public MyDatabaseHelper(Context context, String name, CursorFactory factory, int version) {
        if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);
        mContext = context;
        mName = name;
        mFactory = factory;
        mNewVersion = version;
    }

    public synchronized SQLiteDatabase getWritableDatabase() {
        if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
            return mDatabase;  // The database is already open for business
        }
        if (mIsInitializing) {
            throw new IllegalStateException("getWritableDatabase called recursively");
        }
 
        boolean success = false;
        SQLiteDatabase db = null;
        try {
            mIsInitializing = true;
            if (mName == null) {
                db = SQLiteDatabase.create(null);
            } else {
                    String path = getDatabasePath(mName).getPath();
                db = SQLiteDatabase.openOrCreateDatabase(path, mFactory);
            }
            int version = db.getVersion();
            if (version != mNewVersion) {
                db.beginTransaction();
                try {
                    if (version == 0) {
                        onCreate(db);
                    } else {
                        onUpgrade(db, version, mNewVersion);
                    }
                    db.setVersion(mNewVersion);
                    db.setTransactionSuccessful();
                } finally {
                    db.endTransaction();
                }
            }
            onOpen(db);
            success = true;
            return db;
        } finally {
            mIsInitializing = false;
            if (success) {
                if (mDatabase != null) {
                    try { mDatabase.close(); } catch (Exception e) { }
                }
                mDatabase = db;
            } else {
                if (db != null) db.close();
            }
        }
    }

    public synchronized SQLiteDatabase getReadableDatabase() {
        if (mDatabase != null && mDatabase.isOpen()) {
            return mDatabase;  // The database is already open for business
        }
        if (mIsInitializing) {
            throw new IllegalStateException("getReadableDatabase called recursively");
        }
        try {
            return getWritableDatabase();
        } catch (SQLiteException e) {
            if (mName == null) throw e;  // Can't open a temp database read-only!
            Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
        }
        SQLiteDatabase db = null;
        try {
            mIsInitializing = true;
            String path = getDatabasePath(mName).getPath();
            db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READWRITE);
            if (db.getVersion() != mNewVersion) {
                throw new SQLiteException("Can't upgrade read-only database from version " +
                        db.getVersion() + " to " + mNewVersion + ": " + path);
            }
            onOpen(db);
            Log.w(TAG, "Opened " + mName + " in read-only mode");
            mDatabase = db;
            return mDatabase;
        } finally {
            mIsInitializing = false;
            if (db != null && db != mDatabase) db.close();
        }
    }
 
    public synchronized void close() {
        if (mIsInitializing) throw new IllegalStateException("Closed during initialization");
        if (mDatabase != null && mDatabase.isOpen()) {
            mDatabase.close();
            mDatabase = null;
        }
    }
  
  
  /*public File getDatabasePath(String name)
    *{
      *      return new File("/sdcard/Project/" + name);
    *}
 */
    public abstract File getDatabasePath(String name); //
我们需要添加的只是这一句
    
    public abstract void onCreate(SQLiteDatabase db);

    public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);

    public void onOpen(SQLiteDatabase db) {}
}

你可能感兴趣的:(android,android,android)