Not empty LiteSQL DB at start

前端 未结 1 896
广开言路
广开言路 2021-01-17 06:05

I think that this is rather easy question. I am too young in android stuff already. I want to prepare application which will be using database. In every example I\'ve shown,

相关标签:
1条回答
  • 2021-01-17 06:31
    1. put your filled database in Package's Asset directory,

    2. at application runtime just copy that database to application's internal storage like data/data/<package name>/database directory.

    3. then use it.

    EDIT: this for copy database from asset directory to database directory,

    private void copyDataBase() throws IOException {
    
            try {
                // Open your local db as the input stream
                InputStream myInput = myContext.getAssets().open("your Database file name");
    
                // Path to the just created empty db
                String outFileName = "/data/data/<package name>/databases/";
                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();
    
            } catch (Exception e) {
    
                Log.e("error", e.toString());
    
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题