Sqlite创建database的两种方法,以及源码分析,以及抽象类如何在SqliteHelper中应用

一.创建db两种方法
1.SqilteOpenHelper

public class MyDbHelper extends SQLiteOpenHelper{

    public MyDbHelper(Context context,int version) {
        super(context, "testMusic1.db", null, version);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,  
                          int newVersion) {
    }

}

注意还要用getWriteableDatabase或者是getReadableDatabase来获取db对象:

        MyDbHelper dbHelper = new MyDbHelper(MainActivity.this,1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();


SQLiteDataBase源码分析从getWritableDatabase入手:

   public SQLiteDatabase getWritableDatabase() {
        synchronized (this) {
            return getDatabaseLocked(true);
        }
    }

再是 getDatabaseLocked(true) --->
    private SQLiteDatabase getDatabaseLocked(boolean writable) {
        if (mDatabase != null) {
            if (!mDatabase.isOpen()) {
                // Darn!  The user closed the database by calling mDatabase.close().
                mDatabase = null;
            } else if (!writable || !mDatabase.isReadOnly()) {
                // The database is already open for business.
                return mDatabase;
            }
        }

        if (mIsInitializing) {
            throw new IllegalStateException("getDatabase called recursively");
        }

        SQLiteDatabase db = mDatabase;
        try {
            mIsInitializing = true;

            if (db != null) {
                if (writable && db.isReadOnly()) {
                    db.reopenReadWrite();
                }
            } else if (mName == null) {
                db = SQLiteDatabase.create(null);
            } else {
                try {
                    if (DEBUG_STRICT_READONLY && !writable) {
                        final String path = mContext.getDatabasePath(mName).getPath();
                        db = SQLiteDatabase.openDatabase(path, mFactory,
                                SQLiteDatabase.OPEN_READONLY, mErrorHandler);
                    } else {
                        db = mContext.openOrCreateDatabase(mName, mEnableWriteAheadLogging ?
                                Context.MODE_ENABLE_WRITE_AHEAD_LOGGING : 0,
                                mFactory, mErrorHandler);
                    }
                } catch (SQLiteException ex) {
                    if (writable) {
                        throw ex;
                    }
                    Log.e(TAG, "Couldn't open " + mName
                   + " for writing (will try read-only):", ex);
 final String path = mContext.getDatabasePath(mName).getPath();
 db = SQLiteDatabase.openDatabase(path, mFactory,
                 SQLiteDatabase.OPEN_READONLY, mErrorHandler);
                }
            }

            onConfigure(db);

            final int version = db.getVersion();
            if (version != mNewVersion) {
                if (db.isReadOnly()) {
                    throw new SQLiteException("Can't upgrade read-only database from version "+ db.getVersion() + " to " + mNewVersion + ": " + mName);
                }

                db.beginTransaction();
                try {
                    if (version == 0) {
                        onCreate(db); //抽象方法
                    } else {
                        if (version > mNewVersion) {
                            onDowngrade(db, version, mNewVersion);
                        } else {
                onUpgrade(db, version, mNewVersion); //抽象方法
                        }
                    }
                    db.setVersion(mNewVersion);
                    db.setTransactionSuccessful();
                } finally {
                    db.endTransaction();
                }
            }

            onOpen(db);

            if (db.isReadOnly()) {
                Log.w(TAG, "Opened " + mName + " in read-only mode");
            }

            mDatabase = db;
            return db;
        } finally {
            mIsInitializing = false;
            if (db != null && db != mDatabase) {
                db.close();
            }
        }
    }

注意 oncreate()和 onUpgrade方法,

public abstract class SQLiteOpenHelper{
 /*** Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen. @param db The database.
 */
    public abstract void onCreate(SQLiteDatabase db);
}

 /** @param db The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */
     public abstract void onUpgrade(SQLiteDatabase db, int      
            oldVersion, int newVersion);

onCreate 和 onUpgrade是抽象方法




2、 SQLiteDatabase.openOrCreateDatabase(filePath, null) 直接创建随意目录

 String filePath = MainActivity.this.getFilesDir().getAbsolutePath()+"/testMusic2.db";

SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(filePath, null);

总结:创建database有两种方法,自己用抽象类封装Helper方法

你可能感兴趣的:(Android知识簿)