资源预览内容
第1页 / 共2页
第2页 / 共2页
亲,该文档总共2页全部预览完了,如果喜欢就下载吧!
资源描述
AndroidAndroid 开发之引用外部数据库操作讲解开发之引用外部数据库操作讲解AndroidAndroid 引用外部引用外部数据库数据库( (一一) )有现成的数据库,需要直接引入到项目中使用。# #准备准备在开始之前我们要确认现有的数据库的表结构和字段信息等。(注意要看清楚数据库的大小,后面有用)捕鱼游戏 http:/www.44771.net# #第一步第一步将外部数据库拷贝到项目中的 assets 文件夹中,如图# #第二步第二步在你要使用数据库之前将数据库拷贝到 /data/data/包名/databases/ 目录下。代码代码? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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(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_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 SqlLiteHelper(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 = query.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:/www.77884.net
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号