Android 内存数据库

Android数据库通常以文件的形式存储在磁盘中,而内存数据库是将数据驻留在内存中,因此可以作为一种缓存技术方案。 那么在android如何使用sqlite的内存数据库呢?

看SQLiteDatabase的源码:

[java]  view plain  copy
  1. /** 
  2.      * Create a memory backed SQLite database.  Its contents will be destroyed 
  3.      * when the database is closed. 
  4.      * 
  5.      * <p>Sets the locale of the database to the  the system's current locale. 
  6.      * Call {@link #setLocale} if you would like something else.</p> 
  7.      * 
  8.      * @param factory an optional factory class that is called to instantiate a 
  9.      *            cursor when query is called 
  10.      * @return a SQLiteDatabase object, or null if the database can't be created 
  11.      */  
  12.     public static SQLiteDatabase create(CursorFactory factory) {  
  13.         // This is a magic string with special meaning for SQLite.  
  14.         return openDatabase(MEMORY_DB_PATH, factory, CREATE_IF_NECESSARY);  
  15.     }  

CREATE_IF_NECESSARY 表示:当数据库不存在时将被创建。  通过方法注释可以知道此方法可以创建内存数据库,并当数据库关闭时数据将被清除。

另外一种方法,请看SQLiteOpenHelper源码:

[java]  view plain  copy
  1. public synchronized SQLiteDatabase getWritableDatabase() {  
  2.           
  3.         boolean success = false;  
  4.         SQLiteDatabase db = null;  
  5.         if (mDatabase != null) mDatabase.lock();  
  6.         try {  
  7.             mIsInitializing = true;  
  8.             if (mName == null) {  
  9.                 db = SQLiteDatabase.create(null);  
  10.             } else {  
  11.                 db = mContext.openOrCreateDatabase(mName, 0, mFactory, mErrorHandler);  
  12.             }  
  13.   
  14.             ...  
  15.   
  16.             onOpen(db);  
  17.             success = true;  
  18.             return db;  
  19.         } finally {  
  20.             mIsInitializing = false;  
  21.             if (success) {  
  22.                 if (mDatabase != null) {  
  23.                     try { mDatabase.close(); } catch (Exception e) { }  
  24.                     mDatabase.unlock();  
  25.                 }  
  26.                 mDatabase = db;  
  27.             } else {  
  28.                 if (mDatabase != null) mDatabase.unlock();  
  29.                 if (db != null) db.close();  
  30.             }  
  31.         }  
  32.     }  

从代码中可以知道,当mName(数据库名称)为null时,将创建内存数据库。

我写了一个demo请看代码:

[java]  view plain  copy
  1. package dw.test;  
  2.   
  3. import android.content.ContentValues;  
  4. import android.database.Cursor;  
  5. import android.database.sqlite.SQLiteDatabase;  
  6. import android.provider.BaseColumns;  
  7. import android.util.Log;  
  8.   
  9. public final class MemoryDbTester {  
  10.       
  11.     private static final String LOG_TAG = MemoryDbTester.class.getSimpleName();  
  12.     private static final String TABLE_NAME = "t_user";  
  13.     private SQLiteDatabase mMemoryDb;  
  14.       
  15.     private MemoryDbTester(){  
  16.         mMemoryDb = createMemoryDb();  
  17.     }  
  18.       
  19.     private static MemoryDbTester sDefault = new MemoryDbTester();  
  20.       
  21.     public static MemoryDbTester getDefault(){  
  22.         return sDefault;  
  23.     }  
  24.       
  25.     public interface Columns extends BaseColumns {  
  26.         public static final String UNAME = "uname";  
  27.     }  
  28.       
  29.     /** 
  30.      * 创建内存数据库 
  31.      */  
  32.     private SQLiteDatabase createMemoryDb(){  
  33.         SQLiteDatabase database = SQLiteDatabase.create(null);  
  34.         String t_user_sql = "CREATE TABLE "+TABLE_NAME+"(_id integer primary key autoincrement,"+Columns.UNAME+" varchar(10))";  
  35.         database.execSQL(t_user_sql);  
  36.         return database;  
  37.     }  
  38.     /** 
  39.      * 向内存数据库中插入一条数据 
  40.      */  
  41.     public void testInsert() {  
  42.           
  43.         SQLiteDatabase db = mMemoryDb;  
  44.           
  45.         check(db);  
  46.           
  47.         ContentValues values = new ContentValues();  
  48.         values.put(Columns.UNAME, "dw");  
  49.           
  50.         db.insert(TABLE_NAME, null, values);  
  51.           
  52.     }  
  53.     /** 
  54.      * 查询内存数据库中的数据 
  55.      */  
  56.     public void testQuery(){  
  57.           
  58.         SQLiteDatabase db = mMemoryDb;  
  59.           
  60.         check(db);  
  61.           
  62.         Cursor c = db.rawQuery("select uname from t_user"null);  
  63.           
  64.         while(c.moveToNext()){  
  65.             String name = c.getString(0);  
  66.             Log.i(LOG_TAG, "NAME:" + name);  
  67.         }  
  68.           
  69.     }  
  70.     @Override  
  71.     protected void finalize() throws Throwable {  
  72.           
  73.         releaseMemory();  
  74.           
  75.         super.finalize();  
  76.     }  
  77.       
  78.     public void releaseMemory(){  
  79.         SQLiteDatabase db = mMemoryDb;  
  80.         if(db!=null){  
  81.             db.close();  
  82.             mMemoryDb = null;  
  83.         }  
  84.     }  
  85.       
  86.     private void check(SQLiteDatabase db) {  
  87.         if(db==null || !db.isOpen()){  
  88.             throw new IllegalStateException("memory database already closed");  
  89.         }  
  90.     }  
  91.   
  92. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值