当前位置: 首页 > news >正文

山西网站建设设计网站设计酷站

山西网站建设设计,网站设计酷站,nas 建网站,景观设计网站推荐最近项目需要通过电话号码查询归属地#xff0c;决定直接在本地sqlite的数据库进行查询#xff0c;没必要去访问网络(虽然最后还是决定还是将数据库放在服务器上)一 首先先将外部的sqlite数据库放入assets文件夹下面注意#xff1a;必须是sqlite数据库(XXX.db)#xff0c;如…最近项目需要通过电话号码查询归属地决定直接在本地sqlite的数据库进行查询没必要去访问网络(虽然最后还是决定还是将数据库放在服务器上 )一 首先先将外部的sqlite数据库放入assets文件夹下面注意必须是sqlite数据库(XXX.db)如果是access(XXX.mdb)的必须进行转换网上有一些数据库转换器或者使用SQLite Expert二 创建AssetsDatabaseManager用来获得操作数据库的SQLiteDatabasepackage com.ld.qmwj.dao.phonenumserach;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 QMWJ; // for LogCatprivate static String databasepath /data/data/%s/database; // %s is packageName// A mapping from assets database file to SQLiteDatabase objectprivate Map databases new HashMap();// Context of applicationprivate Context context null;// Singleton Patternprivate 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(contextnull)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 validif(!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 i0; iif(mInstance.databases.get(i)!null){mInstance.databases.get(i).close();}}mInstance.databases.clear();}}}在getDatabase中第一次先将assets中的数据库拷贝到当前工程下(/data/data/[your application package name]/database/数据库名称)以后就可以直接在database中获得该数据库的SQLiteDatabase从而进行增删改查操作了三 使用// 初始化只需要调用一次AssetsDatabaseManager.initManager(getApplication());// 获取管理对象因为数据库需要通过管理对象才能够获取AssetsDatabaseManager mg AssetsDatabaseManager.getManager();// 通过管理对象获取数据库SQLiteDatabase db1 mg.getDatabase(data.db);// 对数据库进行操作db1.execSQL(XXXXXX);
http://www.pierceye.com/news/300392/

相关文章:

  • php 创建网站开发织梦网站后台打不开
  • 网站建设的企业html 网站
  • 山东省建设安全生产协会网站零基础学网站建设 知乎
  • 门户网站做等保需要备案哪些石家庄以岭药业股份有限公司招聘
  • 工信部网站备案方法红豆网桂林论坛
  • 开发网站年度工作总结及明年工作计划免费sem工具
  • 教育网站开发报告郑州哪家医院看男科比较专业
  • 企业门户网站系统wordpress 商业
  • 信金在线制作网站宁波网站建设有限公司
  • 乌海品牌网站建设如何做请求队列防止网站高并发
  • 企业网站开发平台做的网站上传到服务器
  • 企业网站开发开题报告青岛app软件开发
  • 静态网站 搜索合肥做双语外贸网站
  • 网站建设需求计划书手机应用市场
  • 乐清网站自助网站系统
  • 北京4网站建设WordPress不会php
  • wordpress百度搜索对接免费seo提交工具
  • 网站结构优化包括哪些WordPress多城市
  • 北京三屏网站制作wordpress 分词插件
  • 潍坊哪里可以做网站建设网站一定需要云服务器么
  • 高新网站建设多少钱重庆模板网站建设费用
  • 陕西的网站建设公司哪家好网站建设与维护典型案例
  • 遵义网站开发的公司有哪些网站建设vps
  • 百度网站与推广徐州网站建设网络推广
  • 8818网站建设网站建设平台一般多少钱
  • 万网怎样做网站调试百度在线扫一扫
  • 建设网站平台哪个好如何提高商城网站权重
  • 深圳手机网站设计抖音开放平台
  • 平山做网站优化网页版梦幻西游五色石攻略
  • 字体图标制作网站长沙网站排名公司哪家好