Fastest and most efficient way to pre-populate database in Android

后端 未结 7 1375
清歌不尽
清歌不尽 2020-12-28 19:43

If you want to pre-populate a database (SQLite) in Android, this is not that easy as one might think.

So I found this tutorial which is often referenced here on Stac

相关标签:
7条回答
  • 2020-12-28 20:22

    ye, the assets maybe has size limit, so if bigger than the limit, you can cut to more files.

    and exesql support more sql sentence, here give you a example:

        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(asManager.open(INIT_FILE)), 1024 * 4);
            String line = null;
            db.beginTransaction();
            while ((line = br.readLine()) != null) {
                db.execSQL(line);
            }
            db.setTransactionSuccessful();
        } catch (IOException e) {
            FLog.e(LOG_TAG, "read database init file error");
        } finally {
            db.endTransaction();
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    FLog.e(LOG_TAG, "buffer reader close error");
                }
            }
        }
    

    above example require the INIT_FILE need every line is a sql sentence.

    Also, if your sql sentences file is big, you can create the database out site of android(sqlite support for windows, linux, so you can create the database in your os, and copy the database file to your assets folder, if big, you can zip it)

    when your application run, you can get the database file from assets, directed to save to your application's database folder (if you zip it, you can unzip to the application's database folder)

    hope can help you -):

    0 讨论(0)
提交回复
热议问题