在android中數據庫通常以文件的形式存儲在磁盤中,而內存數據庫是將數據駐留在內存中,因此可以作爲一種緩存技術方案。 那麼在android如何使用sqlite的內存數據庫呢?
看SQLiteDatabase的源碼:
/** * Create a memory backed SQLite database. Its contents will be destroyed * when the database is closed. * * <p>Sets the locale of the database to the the system's current locale. * Call {@link #setLocale} if you would like something else.</p> * * @param factory an optional factory class that is called to instantiate a * cursor when query is called * @return a SQLiteDatabase object, or null if the database can't be created */ public static SQLiteDatabase create(CursorFactory factory) { // This is a magic string with special meaning for SQLite. return openDatabase(MEMORY_DB_PATH, factory, CREATE_IF_NECESSARY); }
CREATE_IF_NECESSARY 表示:當數據庫不存在時將被創建。 通過方法註釋可以知道此方法可以創建內存數據庫,並當數據庫關閉時數據將被清除。
另外一種方法,請看SQLiteOpenHelper源碼:
public synchronized SQLiteDatabase getWritableDatabase() { boolean success = false; SQLiteDatabase db = null; if (mDatabase != null) mDatabase.lock(); try { mIsInitializing = true; if (mName == null) { db = SQLiteDatabase.create(null); } else { db = mContext.openOrCreateDatabase(mName, 0, mFactory, mErrorHandler); } ... onOpen(db); success = true; return db; } finally { mIsInitializing = false; if (success) { if (mDatabase != null) { try { mDatabase.close(); } catch (Exception e) { } mDatabase.unlock(); } mDatabase = db; } else { if (mDatabase != null) mDatabase.unlock(); if (db != null) db.close(); } } }
從代碼中可以知道,當mName(數據庫名稱)爲null時,將創建內存數據庫。
我寫了一個demo請看代碼:
package dw.test; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.provider.BaseColumns; import android.util.Log; public final class MemoryDbTester { private static final String LOG_TAG = MemoryDbTester.class.getSimpleName(); private static final String TABLE_NAME = "t_user"; private SQLiteDatabase mMemoryDb; private MemoryDbTester(){ mMemoryDb = createMemoryDb(); } private static MemoryDbTester sDefault = new MemoryDbTester(); public static MemoryDbTester getDefault(){ return sDefault; } public interface Columns extends BaseColumns { public static final String UNAME = "uname"; } /** * 創建內存數據庫 */ private SQLiteDatabase createMemoryDb(){ SQLiteDatabase database = SQLiteDatabase.create(null); String t_user_sql = "CREATE TABLE "+TABLE_NAME+"(_id integer primary key autoincrement,"+Columns.UNAME+" varchar(10))"; database.execSQL(t_user_sql); return database; } /** * 向內存數據庫中插入一條數據 */ public void testInsert() { SQLiteDatabase db = mMemoryDb; check(db); ContentValues values = new ContentValues(); values.put(Columns.UNAME, "dw"); db.insert(TABLE_NAME, null, values); } /** * 查詢內存數據庫中的數據 */ public void testQuery(){ SQLiteDatabase db = mMemoryDb; check(db); Cursor c = db.rawQuery("select uname from t_user", null); while(c.moveToNext()){ String name = c.getString(0); Log.i(LOG_TAG, "NAME:" + name); } } @Override protected void finalize() throws Throwable { releaseMemory(); super.finalize(); } public void releaseMemory(){ SQLiteDatabase db = mMemoryDb; if(db!=null){ db.close(); mMemoryDb = null; } } private void check(SQLiteDatabase db) { if(db==null || !db.isOpen()){ throw new IllegalStateException("memory database already closed"); } } }