How to insert data into the database file which is in the assets folder

前端 未结 3 902
不思量自难忘°
不思量自难忘° 2020-12-18 13:30

I have created a SQLite file and putted it on to assets folder. Now I am able to read the data from that file but I don\'t have any idea how to write to this database file.

3条回答
  •  爱一瞬间的悲伤
    2020-12-18 14:22

    you need to copy database file from asset and paste in /data/data/''/databases/ folder

    private static String DB_NAME = "QuotesData.db";
    private static String DB_PATH = "/data/data/[PACKAGE NAME]/databases/";
    private void copyDataBase() throws IOException {
    
            InputStream myInput = context.getAssets().open(DB_NAME);
    
            String outFileName = DB_PATH + DB_NAME;
    
            OutputStream myOutput = new FileOutputStream(outFileName);
    
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
    
            myOutput.flush();
            myOutput.close();
            myInput.close();
    
        }
    

    In Above code : Replace [PACKAGE NAME ] with your package name I hope above code will help.

    Thank You

提交回复
热议问题