首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

读取assets也许raw目录下的sqlite数据库

2013-08-16 
读取assets或者raw目录下的sqlite数据库public class DbManager{private static final int BUFFER_SIZE

读取assets或者raw目录下的sqlite数据库
public class DbManager{private static final int BUFFER_SIZE = 2046;public static final String DB_NAME = "school_out.db";public static final String DB_PATH = "/data"+ android.os.Environment.getDataDirectory().getAbsolutePath()+ "包名" + "/";public static final String DB_FILE = DB_PATH + DB_NAME;private Context mContext;private SQLiteDatabase database;public DbManager(Context context){super();this.mContext = context;}public void openDatabase(){closeDatebase();database = openDateBase();}/** * * <b>功能名:</b> 打开数据库 <br/> * <b>功能描述:</b> <br/> * <b>创建时间:</b> 2013-8-6 <br/> * <b>修改时间:</b> 2013-8-6 <br/> * * @return */public SQLiteDatabase openDateBase(){File file = new File(DB_FILE);// 如果文件不存在,将 raw 下的db文件复制到 data/data下面if (!file.exists()){File filePath = new File(DB_PATH);if(!file.exists())filePath.mkdirs();// // 打开raw中得数据库文件,获得stream流InputStream stream = this.mContext.getResources().openRawResource(R.raw.school_out);try{// 将获取到的stream 流写入道data中FileOutputStream outputStream = new FileOutputStream(DB_FILE);byte[] buffer = new byte[BUFFER_SIZE];int count = 0;while ((count = stream.read(buffer)) > 0){outputStream.write(buffer, 0, count);}outputStream.close();stream.close();}catch (FileNotFoundException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}}SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(DB_FILE,null);return database;}public SchoolBean getBean(String schoolName){SchoolBean bean = null;Cursor cursor = this.database.rawQuery("SELECT * FROM school_out where name = '" + schoolName + "'", null);try{int index = cursor.getCount();System.out.println(index);if(index != 0){cursor.moveToFirst();bean = new SchoolBean();bean.setId(cursor.getString(cursor.getColumnIndex("sid")));bean.setSchoolName(cursor.getString(cursor.getColumnIndex("name"))); }}catch (Exception e){e.printStackTrace();}finally{cursor.close();}return bean;}/** * 获取学校数据(拉取10 条) * */public ArrayList<SchoolBean> getSchoolList(String where){ArrayList<SchoolBean> nameList = new ArrayList<SchoolBean>();Cursor cursor = this.database.rawQuery("SELECT * FROM school_out where name like '%" + where+ "%' limit 0, 20", null);try{for (int i = 0; i < cursor.getCount(); i++){cursor.moveToPosition(i);SchoolBean bean = new SchoolBean();bean.setId(cursor.getString(cursor.getColumnIndex("sid")));bean.setSchoolName(cursor.getString(cursor.getColumnIndex("name")));nameList.add(bean);}}catch (Exception e){e.printStackTrace();}finally{cursor.close();}return nameList;}//public SchoolBean[] getSchoolData(String where)//{//ArrayList<SchoolBean> lists = getSchoolList(where);//return (SchoolBean[]) lists.toArray();//}/** * * <b>功能名:</b> 关闭数据库 <br/> * <b>功能描述:</b> <br/> * <b>创建时间:</b> 2013-8-6 <br/> * <b>修改时间:</b> 2013-8-6 <br/> */public void closeDatebase(){if (database != null && database.isOpen()){database.close();}}}

?

热点排行