Android 使用存放在存assets文件夹下的SQLite数据库

因为这次的项目需要自带数据,所以就就把数据都放到一个SQLite的数据库文件中了,之后把该文件放到了assets文件夹下面。一开始打算每次都从assets文件夹下面把该文件夹拷贝到手机的SD卡或者手机自身的存储上之后再使用,后来考虑到每次都拷贝的话效率不高,并且如果涉及到对数据库的修改操作的话拷贝之后数据就被恢复了。

因此就写了该封装,该封装只是在第一次使用数据库文件的时候把该文件夹拷贝到手机的/data/data/应用程序报名/database文件夹下,之后就直接从这个地方使用了。并且它允许你直接通过assets文件夹下的数据库名称来获取SQLiteDatabase对象,这样就极大的方便了你对数据库的使用。

package com.jemsn.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 is a Assets Database Manager
 * Use it, you can use a assets database file in you application
 * It will copy the database file to "/data/data/[your application package name]/database" when you first time you use it
 * Then you can get a SQLiteDatabase object by the assets database file 
 * @author RobinTang
 * @time 2012-09-20
 * 
 * 
 * How to use:
 * 1. Initialize AssetsDatabaseManager
 * 2. Get AssetsDatabaseManager
 * 3. Get a SQLiteDatabase object through database file
 * 4. Use this database object
 * 
 * Using example:
 * AssetsDatabaseManager.initManager(getApplication());	// this method is only need call one time
 * AssetsDatabaseManager mg = AssetsDatabaseManager.getManager();	// get a AssetsDatabaseManager object
 * SQLiteDatabase db1 = mg.getDatabase("db1.db");	// get SQLiteDatabase object, db1.db is a file in assets folder
 * db1.???	// every operate by you want
 * Of cause, you can use AssetsDatabaseManager.getManager().getDatabase("xx") to get a database when you 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, SQLiteDatabase> databases = new HashMap<String, SQLiteDatabase>();
	
	// Context of application
	private Context context = null;
	
	// Singleton Pattern
	private static AssetsDatabaseManager mInstance = null;
	
	/**
	 * Initialize AssetsDatabaseManager
	 * @param context, context of application
	 */
	public static void initManager(Context context){
		if(mInstance == null){
			mInstance = new AssetsDatabaseManager(context);
		}
	}
	
	/**
	 * Get a AssetsDatabaseManager object
	 * @return, if success return a AssetsDatabaseManager object, else return null
	 */
	public static AssetsDatabaseManager getManager(){
		return mInstance;
	}
	
	private AssetsDatabaseManager(Context context){
		this.context = context;
	}
	
	/**
	 * Get a assets database, if this database is opened this method is only return a copy of the opened database
	 * @param dbfile, the assets file which will be opened for a database
	 * @return, if success it return a SQLiteDatabase 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 was copied and valid
		if(!flag || !file.exists()){
			file = 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, sfile));
				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().packageName);
	}
	
	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 will be closed soon
	 * @return, the status 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, "closeAllDatabase");
		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();
		}
	}
}
使用的过程也很简单,应用程序开始的时候初始化一下,之后就可以在任意地方获取管理器在通过assets文件夹下的数据库文件名直接获取SQLiteDatabase对象,之后对数据库的操作就完全看你了。。。

简单的使用例子:

// 初始化,只需要调用一次
AssetsDatabaseManager.initManager(getApplication());
// 获取管理对象,因为数据库需要通过管理对象才能够获取
AssetsDatabaseManager mg = AssetsDatabaseManager.getManager();
// 通过管理对象获取数据库
SQLiteDatabase db1 = mg.getDatabase("db1.db");
// 对数据库进行操作
db1.execSQL("insert into tb([ID],[content]) values(null, 'db1');");
需要注意的是获取数据库对象的时候是区分数据库文件名的大小写的。


Android 使用存放在存assets文件夹下的SQLite数据库,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。