安卓copy assets目录下的数据库到data/data/...目录下

1.在assets中准备好数据库文件
在这里插入图片描述

2.创建一个工具类用来copy数据库

public class CopyDBUtils {

    private static InputStream is;
    private static FileOutputStream fos;

    public static void copyDbFile(Context context, String dbName) {

        //databases文件夹目录
        String path = "/data/data/" + context.getPackageName() + "/databases";

        //后面通过SQLiteDatabase.openDatabas获取数据库
        File file = new File(path + "/" + dbName);
        try {
            //创建文件夹(如果之前没有创建过数据库,data/data/包名/下的databases目录是不存在的)
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            /*
            * mkdir() 与 mkdirs() 的区别
            * mkdirs()可以创建多级目录文件
            * 比如要在 C:\a\b\ 目录下创建一个文件test.txt  (此时不存在文件夹a和b)
            *  mkdirs()就会先创建文件夹a,文件夹b。最后创建test.txt问价
            * 但是mkdir()则创建失败,返回创建结果false
            * */
            if (file.exists())//删除已经存在的
                file.deleteOnExit();
            //创建新的文件
            if (!file.exists())
                file.createNewFile();
            // 从assets目录下复制
            is = context.getAssets().open(dbName + ".db");
            fos = new FileOutputStream(file);
            int len = 0;
            byte[] b = new byte[1024];
            while ((len = is.read(b)) != -1) {
                fos.write(b, 0, len);
            }
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) is.close();
                if (fos != null) fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

    }

}

3.0创建一个JavaBean封装数据

public class Virus {
    private String md5;
    private int type;
    private String name;
    private String desc;

    public String getMd5() {
        return md5;
    }

    public void setMd5(String md5) {
        this.md5 = md5;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

3.创建一个Dao类来操作数据库

public class VirusDao {
    public VirusDao(Context context,String dbName){
        CopyDBUtils.copyDbFile(context,dbName);
    }

    private SQLiteDatabase getDatabase(Context context,String DBName){
        String path = "/data/data/" + context.getPackageName() + "/databases"+ "/" + DBName;
        return SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
    }

    public List<Virus> queryAll(Context context,String DBName){
        SQLiteDatabase db = getDatabase(context, DBName);
        Cursor cursor = db.query("datable", null, null, null, null, null, null);
        List<Virus> list = new ArrayList<>();
        while (cursor.moveToNext()){
            Virus virus = new Virus();
            String md5 = cursor.getString(1);
            virus.setMd5(md5);
            int type = cursor.getInt(2);
            virus.setType(type);
            String name = cursor.getString(3);
            virus.setName(name);
            String desc = cursor.getString(4);
            virus.setDesc(desc);
            list.add(virus);
        }
        cursor.close();
        db.close();

        return list;
    }
}

4.测试

//activity中的按钮点击事件,由于数据太多,就输出了三条
public void testDB(View view) {
        final VirusDao dao = new VirusDao(VirusScanActivity.this,"antivirus");
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<Virus> list = dao.queryAll(VirusScanActivity.this, "antivirus");
                int i = 3;
                for(Virus virus:list){
                    if(i == 0){
                        return;
                    }
                    Log.d(TAG,"md5:"+virus.getMd5()+";"+"desc"+virus.getDesc());
                    i--;
                }
            }
        }).start();
    }
}

测试结果:

D/VirusScanActivity: md5:a2bd62c99207956348986bf1357dea01;desc恶意后台扣费,病毒木马程序
                     md5:ac365eeb5595554d67975ad61003e48e;desc恶意后台扣费,病毒木马程序
                     md5:30f8c5d2cc445273e959b2a49fc8e937;desc恶意后台扣费,病毒木马程序
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:使用npm命令或输入npm -v出现错误internal/modules/cjs/loader.js:888 throw errError: Cannot find module 'are-we-there-yet'解决。\[1\]这个错误通常是由于缺少依赖模块引起的。你可以尝试以下解决方法: 1. 确保你的项目目录下有package.json文件,其包含了你的项目所需的依赖信息。 2. 运行npm install命令,以安装项目所需的依赖模块。 3. 如果上述方法无效,可以尝试删除node_modules文件夹,并重新运行npm install命令,以重新安装依赖模块。 引用\[2\]:npm audit fix --force npm install optimize-css-assets-webpack-plugin@3.2.0 npm i copy-webpack-plugin。\[2\]这是一种解决npm依赖安全漏洞的方法。npm audit fix命令可以自动修复一些已知的安全漏洞,而--force选项可以强制执行修复。另外,npm install命令可以用来安装特定版本的依赖模块,例如optimize-css-assets-webpack-plugin@3.2.0和copy-webpack-plugin。 引用\[3\]:\node_modules\copy-webpack-plugin\dist\index.js:56 compiler.hooks.emit.tapAsync(plugin, (compilation, callback) => { ^。\[3\]这个错误通常是由于copy-webpack-plugin插件的版本不兼容导致的。你可以尝试更新copy-webpack-plugin插件的版本,或者查看插件的文档以了解是否有其他解决方法。另外,你还可以尝试删除node_modules文件夹,并重新运行npm install命令,以重新安装依赖模块。 #### 引用[.reference_title] - *1* [internal/modules/cjs/loader.js:888 throw errError: Cannot find module ‘are-we-there-yet](https://blog.csdn.net/qq_44774831/article/details/117932649)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [关于vue 用 npm run build 出现的一系列问题解决记录 webpack打包出现的问题等等](https://blog.csdn.net/NapoleonCesar/article/details/124986324)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值