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

前端之家收集整理的这篇文章主要介绍了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,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,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,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是抽象方法@H_404_236@ 

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

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

sqliteDatabase db = sqliteDatabase.openOrCreateDatabase(filePath,null);@H_404_236@ 

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

原文链接:https://www.f2er.com/sqlite/198328.html

猜你在找的Sqlite相关文章