Not empty LiteSQL DB at start

2019-02-21 06:14发布

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, there is an empty database where application is firstly started and after that there are some inserts. I want to have app with rather big db so I want to have filled db when app is started. How can I prepare db and attach it to program?

1条回答
贼婆χ
2楼-- · 2019-02-21 06:53
  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());

        }

    }
查看更多
登录 后发表回答