The example in this article describes how the Android operation SQLite the database under the Assets folder. Share to everyone for your reference. Specifically as follows:
Because this project needs to bring data, so put the data into a SQLite database file, then put the file under the Assets folder. The first plan is to copy the folder from the assets folder to the phone's SD card or the phone's own storage every time, and then take into account that every time the copy is not efficient, and if it involves the modification of the database copy after the data is restored.
So the package is written, which is only used when the database file is first used to copy the folder to the phone's/data/data/application registration/database folder, and then directly from this place. And it allows you to get the Sqlitedatabase object directly through the name of the database under the Assets folder, which greatly facilitates your use of the database.
The package is as follows:
Package com.sin.android.database;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.util.HashMap;
Import Java.util.Map;
Import Android.content.Context;
Import android.content.SharedPreferences;
Import Android.content.res.AssetManager;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.util.Log; /** * This are a Assets database Manager * Use it, can be use a Assets database file with you application * It'll co PY the database file to "/data/data/[your application package Name]/database" When your use it * Then Can get a Sqlitedatabase object by the assets database file * @author Robintang * @time 2012-09-20 * * * * SE: * 1. Initialize Assetsdatabasemanager * 2. Get Assetsdatabasemanager * 3. Get a Sqlitedatabase object through database file * 4. Use the This database object * * Using Example: * Assetsdatabasemanager.initmanager (getapplication()); This are only need call one time * Assetsdatabasemanager mg = Assetsdatabasemanager.getmanager (); Get a Assetsdatabasemanager object * sqlitedatabase db1 = mg.getdatabase ("db1.db"); Get Sqlitedatabase object, db1.db are a file in assets folder * db1.??? Every operate by your want * of cause, can use Assetsdatabasemanager.getmanager (). Getdatabase ("xx") to get a datab ASE need use a database/public class Assetsdatabasemanager {private static String tag = "Assetsdatabase "; For logcat private static String DatabasePath = "/data/data/%s/database"; %s is PackageName//A mapping from assets database file to Sqlitedatabase object private map<string, Sqliteda
tabase> databases = new hashmap<string, sqlitedatabase> ();
Context of application Private context = NULL;
Singleton pattern private static Assetsdatabasemanager minstance = null; /** * Initialize Assetsdatabasemanager * @parAm context, the context of application */public static void InitManager {if (minstance = null) {
Minstance = new Assetsdatabasemanager (context); }/** * Get a Assetsdatabasemanager object * @return, if success return a Assetsdatabasemanager object, El
SE return NULL */public static Assetsdatabasemanager GetManager () {return minstance;
Private Assetsdatabasemanager {this.context = context;
}/** * Get a assets database, if it is opened this method was only return a copy of the opened database * @param DBFile, the assets file which is opened for a database * @return, if success it return a Sqlitedatab ASE Object else return NULL */public sqlitedatabase getdatabase (String dbfile) {if (Databases.get (dbfile)!=
NULL) {LOG.I (tag, String.Format ("Return a database copy of%s", DBFile));
Return (sqlitedatabase) databases.get (DBFile);
} if (context==null) return null;
LOG.I (Tag, String.Format ("Create database%s", dbfile));
String spath = Getdatabasefilepath ();
String sfile = Getdatabasefile (DBFile);
File File = new file (sfile);
Sharedpreferences dbs = context.getsharedpreferences (AssetsDatabaseManager.class.toString (), 0); Boolean flag = Dbs.getboolean (DBFile, false); Get database file flag, if true means this database file is copied and valid if (!flag | |!file.exists ()) {F
ile = new File (spath);
if (!file.exists () &&!file.mkdirs ()) {LOG.I (tag, "Create \" "+spath+" \ "fail!");
return null; } if (!copyassetstofilesystem (DBFile, sfile)) {LOG.I (tag, String.Format ("Copy%s to%s fail!," DBFile, Sfil
e));
return null;
} dbs.edit (). Putboolean (DBFile, True). commit ();
} sqlitedatabase db = Sqlitedatabase.opendatabase (sfile, NULL, sqlitedatabase.no_localized_collators); if (db!= null) {
Databases.put (dbfile, DB);
} return DB; Private String Getdatabasefilepath () {return String.Format (DatabasePath, Context.getapplicationinfo (). Packagenam
e);
private string Getdatabasefile (string dbfile) {return Getdatabasefilepath () + "/" +dbfile;
Private Boolean Copyassetstofilesystem (String assetssrc, String des) {LOG.I (tag, "Copy" +assetssrc+ "to" +des);
InputStream istream = null;
OutputStream ostream = null;
try{Assetmanager am = context.getassets ();
IStream = Am.open (ASSETSSRC);
ostream = new FileOutputStream (DES);
byte[] buffer = new byte[1024];
int length;
while (length = istream.read (buffer)) >0 {ostream.write (buffer, 0, length);
} istream.close ();
Ostream.close ();
catch (Exception e) {e.printstacktrace ();
try{if (istream!=null) istream.close (); if (ostream!=null) ostream. Close ();
catch (Exception ee) {ee.printstacktrace ();
return false;
return true; /** * Close Assets Database * @param dbfile, the assets file which'll be closed soon * @return, the STA
Tus of this operating * * public boolean closeDatabase (String dbfile) {if Databases.get (dbfile)!= null) {
Sqlitedatabase db = (sqlitedatabase) databases.get (DBFile);
Db.close ();
Databases.remove (DBFile);
return true;
return false; }/** * Close all assets database */static public void Closealldatabase () {LOG.I (tag, "Closealldatabas
E "); if (minstance!= null) {for (int i=0; i<minstance.databases.size (); ++i) {if (MInstance.databases.get (i)!=
NULL) {MInstance.databases.get (i). Close ();
} mInstance.databases.clear ();
}
}
}
The use of the process is also very simple, the beginning of the application initialization, and then can be anywhere to get the manager in the Assets folder under the database file name directly get the Sqlitedatabase object, after the operation of the database to completely look at you ...
Simple examples of use:
Initialize, only need to call once
Assetsdatabasemanager.initmanager (Getapplication ());
Gets the management object because the database needs to be managed to obtain
assetsdatabasemanager mg = Assetsdatabasemanager.getmanager ();
Get database
sqlitedatabase db1 = mg.getdatabase ("Db1.db") by admin object;
Manipulate the database
Db1.execsql (INSERT into TB ([id],[content]) VALUES (null, ' db1 ');
Note that when you get a database object, you are sensitive to the case of the database file name.
I hope this article will help you with your Android program.