我是
靠谱客的博主
灵巧背包,最近开发中收集的这篇文章主要介绍
Android 如果读取asserts 中的db文件,并进行查询操作,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
前言:前篇已经讲了,如何将excel表格转化成db文件使用。不清楚的朋友可以看下我上篇的文章。然而在这里,主要说下怎么操作assserts目录下的db文件,进行对数据库的查询操作。废话不多说,开始吧!
步骤:
1,在项目中创建一个asserts 目录,有很多人都是手动添加文件夹,命名asserts,其实studio可以直接创建,如图
2,将创建好的.db文件拷贝到asserts目录,需要将db文件通过流的形式写入手机中,便于打开数据库,具体代码如下:
这里是将文件拷贝到手机里,以及查询数据库的操作
- public class DBManager {
- private String DB_NAME = "demo.db";
- private Context mContext;
-
- public List<Bean> mBeanLists = new ArrayList<Bean>();
- public DBManager(Context mContext) {
- this.mContext = mContext;
- }
-
- public SQLiteDatabase DBManager(String packName) {
- String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/databases/" + DB_NAME;
- if (!new File(dbPath).exists()) {
- try {
- boolean flag = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/databases/").mkdirs();
- boolean newFile = new File(dbPath).createNewFile();
- try {
- FileOutputStream out = new FileOutputStream(dbPath);
- InputStream in = mContext.getAssets().open("demo.db");
- byte[] buffer = new byte[1024];
- int readBytes = 0;
- while ((readBytes = in.read(buffer)) != -1)
- out.write(buffer, 0, readBytes);
- in.close();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return SQLiteDatabase.openOrCreateDatabase(dbPath, null);
- }
-
- public List<Bean> query(SQLiteDatabase sqliteDB, String[] columns, String selection, String[] selectionArgs) {
- Bean bean= null;
- try {
- String table = "bank";
- Cursor cursor = sqliteDB.query(table, columns, selection, selectionArgs, null, null, null);
- while (cursor.moveToNext()) {
- String title = cursor.getString(cursor.getColumnIndex("title"));
- String answer = cursor.getString(cursor.getColumnIndex("answer"));
- String daan1 = cursor.getString(cursor.getColumnIndex("daan1"));
- String daan2 = cursor.getString(cursor.getColumnIndex("daan2"));
- String daan3 = cursor.getString(cursor.getColumnIndex("daan3"));
- String daan4 = cursor.getString(cursor.getColumnIndex("daan4"));
- bean= new Bean();
- bean.setTitle(title);
- bean.setAnswer(answer);
- bean.setDaan1(daan1);
- bean.setDaan2(daan2);
- bean.setDaan3(daan3);
- bean.setDaan4(daan4);
- mBeanLists.add(bean);
- }
- cursor.close();
- return mCityLists;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
3,在主Activity中怎么去查询数据库,以及展示数据呢?需要注意的是(
具体的查询语句,操作语句,耗时操作要放在子线程里处理
)
- new Thread(new Runnable() {
- @Override
- public void run() {
- DBManager dbManager = new DBManager(NextActivity.this);
- SQLiteDatabase sqLiteDatabase = dbManager.DBManager("com.yoyo.myapplication");
- String[] columns = new String[]{"title", "answer","daan1","daan2","daan3","daan4"};
- String selection = "title like '"+"%"+mKey+"%'";
- List<City> mCityLists = dbManager.query(sqLiteDatabase, columns, selection, null);
- Message message = new Message();
- message.what = DAN_XUAN;
- message.obj = mCityLists;
- handler.sendMessage(message);
- sqLiteDatabase.close();
- }
- }).start();
4,查询完毕,回到handler里面做需求操作
- Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- switch (msg.what){
- case DUI_CUO:
- List<DuiCuo> mDuiCuoLists = (List<DuiCuo>) msg.obj;
- Toast.makeText(NextActivity.this, "共" + mDuiCuoLists.size() + "题对错题", Toast.LENGTH_SHORT).show();
- DuiCuoAdapter duiCuoAdapter = new DuiCuoAdapter(NextActivity.this, mDuiCuoLists);
- mLv.setAdapter(duiCuoAdapter);
- break;
- case DAN_XUAN:
- List<City> mDanxuanLists = (List<City>) msg.obj;
- Toast.makeText(NextActivity.this, "共" + mDanxuanLists.size() + "题选择题", Toast.LENGTH_SHORT).show();
- XuanZheAdapter adapter = new XuanZheAdapter(NextActivity.this, mDanxuanLists);
- mLv.setAdapter(adapter);
- break;
- }
- }
- };
最后
以上就是灵巧背包为你收集整理的Android 如果读取asserts 中的db文件,并进行查询操作的全部内容,希望文章能够帮你解决Android 如果读取asserts 中的db文件,并进行查询操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复