安卓加载外部数据库

1,首先把数据库拷贝到如图文件夹
这里写图片描述
2,要把数据库加载到data/data/包名/files/数据库名.db
3,代码


        // 复制数据库 data/data/com.songzhen.mobilesafe/files/address.db
        try {// 加载数据库
            InputStream is = getAssets().open("address.db");
            File file = new File(getFilesDir(), "address.db");
            if (file.exists() && file.length() > 0) {
                Toast.makeText(this, "数据库之前加载完毕", 0).show();
            } else {
                FileOutputStream fos = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                is.close();
                fos.close();
                Toast.makeText(this, "数据库第一次加载完毕", 0).show();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

4,用数据库

String path = "data/data/com.songzhen.mobilesafe/files/address.db";
            SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
            Cursor cursor = sqLiteDatabase.rawQuery(
                    "select location from data2 where id=(select outkey from data1 where id=?)", new String[] { num.substring(0,7) });
            while (cursor.moveToNext()) {
                area = cursor.getString(0);
            }
            cursor.close();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值