Android操作存放在assets文件夹下SQLite数据库的方法
程序员文章站
2023-11-08 14:13:04
本文实例讲述了android操作存放在assets文件夹下sqlite数据库的方法。分享给大家供大家参考。具体如下:
因为这次的项目需要自带数据,所以就就把数据都放到一个...
本文实例讲述了android操作存放在assets文件夹下sqlite数据库的方法。分享给大家供大家参考。具体如下:
因为这次的项目需要自带数据,所以就就把数据都放到一个sqlite的数据库文件中了,之后把该文件放到了assets文件夹下面。一开始打算每次都从assets文件夹下面把该文件夹拷贝到手机的sd卡或者手机自身的存储上之后再使用,后来考虑到每次都拷贝的话效率不高,并且如果涉及到对数据库的修改操作的话拷贝之后数据就被恢复了。
因此就写了该封装,该封装只是在第一次使用数据库文件的时候把该文件夹拷贝到手机的/data/data/应用程序报名/database文件夹下,之后就直接从这个地方使用了。并且它允许你直接通过assets文件夹下的数据库名称来获取sqlitedatabase对象,这样就极大的方便了你对数据库的使用。
封装如下:
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 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程序设计有所帮助。
上一篇: 亚马逊站外推广的思路
下一篇: jQuery.extend 函数详解