how to put database and read database from assets folder android which are created and exported in sqllite

后端 未结 5 1268
耶瑟儿~
耶瑟儿~ 2020-12-15 14:28

I am new in Android development. I am making a database through SQLite Manager 0.8.1. The table Users contains two fields, username and password. I created the database file

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 15:06

    Use the following:

    private void copyDataBase() throws IOException{
    
        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);
    
        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;
    
        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);
    
        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }
    
        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    
    }
    

提交回复
热议问题