安卓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恶意后台扣费,病毒木马程序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值