In Android, a database is typically stored as a file on disk, and the memory database resides in memory, so it can be used as a caching technology scenario. So how does Android use SQLite's in-memory database?
Look at the source of 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&nbsP 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 indicates that the database will be created when it does not exist. You can tell by method annotations that this method can create a memory database and that the data will be purged when the database is closed.
Another way, please see Sqliteopenhelper Source:
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 { &nbSp; db = mcontext.openorcreatedatabase (Mname, 0, mfactory, merrorhandler); } ... OnOpen (db); success = true; return db; } finally { mIsInitializing = false; if (Success) { if (mdatabase != null) { &nbsP; try { mdatabase.close (); } catch ( Exception e) { } mdatabase.unlock (); } mDatabase = db; } else { if (Mdatabase != null) mdatabase.unlock (); if (Db != null) db.close (); } } }
You can tell from the code that when Mname (the database name) is NULL, the memory database is created.
I wrote a demo, please read the code:
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 ();p rivate static final string table_name = "T_user";p rivate sqlitedatabase mmemorydb;private Memorydbtester () {mmemorydb = creatememorydb ();} Private static memorydbtester sdefault = new memorydbtester ();p ublic static memorydbtester getdefault () {return sdefault;} public interface columns extends basecolumns {public static final string uname = "UNAME";} /** * Creating the Memory Database */private sqlitedatabase creatememorydb () {sqlitedatabase database& NBsp;= sqlitedatabase.create (NULL); string t_user_sql = "create table " +table_name+ "(_id integer primary Key autoincrement, "+columns.uname+" varchar ";d atabase.execsql (t_user_sql); return database;} /** * inserts a data into the in-memory Database */public void testinsert () {sqlitedatabase db = mmemorydb;check (DB); Contentvalues values = new contentvalues (); Values.put (columns.uname, "DW");d B.insert ( table_name, null, values);} /** * querying the data in the in-memory Database */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);}} @Overrideprotected void finalize () throws throwable {releasememory (); Super.finalize () ;} Public void&nbSp;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") ;}}}
This article is from the "Itlife" blog, make sure to keep this source http://xwteacher.blog.51cto.com/9653444/1584554
Android Memory Database