Android开发之引用外部数据库操作讲解

上传人:m**** 文档编号:52262094 上传时间:2018-08-19 格式:DOCX 页数:2 大小:19.53KB
返回 下载 相关 举报
Android开发之引用外部数据库操作讲解_第1页
第1页 / 共2页
Android开发之引用外部数据库操作讲解_第2页
第2页 / 共2页
亲,该文档总共2页,全部预览完了,如果喜欢就下载吧!
资源描述

《Android开发之引用外部数据库操作讲解》由会员分享,可在线阅读,更多相关《Android开发之引用外部数据库操作讲解(2页珍藏版)》请在金锄头文库上搜索。

1、AndroidAndroid 开发之引用外部数据库操作讲解开发之引用外部数据库操作讲解AndroidAndroid 引用外部引用外部数据库数据库( (一一) )有现成的数据库,需要直接引入到项目中使用。# #准备准备在开始之前我们要确认现有的数据库的表结构和字段信息等。(注意要看清楚数据库的大小,后面有用)捕鱼游戏 http:/# #第一步第一步将外部数据库拷贝到项目中的 assets 文件夹中,如图# #第二步第二步在你要使用数据库之前将数据库拷贝到 /data/data/包名/databases/ 目录下。代码代码? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

2、16 17 18 19public static void copyDbFile(Context context, String db_name) InputStream in = null;FileOutputStream out = null;/String path = “/data/data/“ + context.getPackageName() + “/databases/“;File filePath = context.getDatabasePath(db_name);/spUtils 是为了防止多次拷贝if (!SharePreferenceUtils.getBoolean(

3、GlobalContent.COPE_SUCCESS,false)try in = context.getAssets().open(db_name); / 从 assets 目录下复制out = new FileOutputStream(filePath); int length = -1; byte buf = new byte1024; while (length = in.read(buf) != -1) out.write(buf, 0, length); out.flush(); SharePreferenceUtils.putBoolean(GlobalContent.COPE_

4、SUCCESS,true);20 21 22 23 24 25 26 27 28 29 catch (Exception e) e.printStackTrace(); finally try if (in != null) in.close();if (out != null) out.close(); catch (IOException e1) e1.printStackTrace(); # #第三步第三步这时就可以开始查库了? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17SqlLiteHelper sqlLiteHelper = new SqlLi

5、teHelper(getContext(), “mySql.db“, null, 1); SQLiteDatabase readableDatabase = sqlLiteHelper.getReadableDatabase();try Cursor query = readableDatabase.query(“message“, new String“_id“, “message“, null, null, null, null, null, limit);boolean b = query.moveToFirst();while (!query.isLast() int id = que

6、ry.getInt(query.getColumnIndex(“_id“); String message = query.getString(query.getColumnIndex(“message“); mDataList.add(new LoveMessageBean(id, message); query.moveToNext();query.close();Logger.i(“mDataList : “+ mDataList.size();catch (Exception e)UiUtils.showToast(getContext(),“error“);到这里已经成功的把外部数据库拷贝到项目中,并且开始 CRUD 了。以上的方法,是做简单也是最原始的方法,之后会尝试使用第三方的工具来查询,如 GreenDao LitePal 等。棋牌评测网 http:/

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 手机/mobile开发

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号