Pre-built database

Starting with Prebuilt Database

In this section, you will learn how to bundle a pre-built Couchbase Lite database in an application. It can be a lot more efficient to bundle static or semi-static content database in your application and install it on the first launch. Even if some of the content changes on the server after you create the app, the app’s first pull replication will bring the database up to date. Here, you will use a pre-built database that contains only airport and hotel documents. The code below moves the pre-built database from the bundled location to the Application Support directory.

Open the file app/src/android/java/…​/util/DatabaseManager.java and navigate to the database constructor.

This method first checks if a database file already exists for specific user. If it doesn’t exist it loads the database from the assets directory, unzips and copies it over to a folder created for ths user.

  public void OpenDatabaseForUser(Context context,String username) {
        File dbFile = new File(context.getFilesDir()+"/"+ username, "travel-sample.cblite2");
        DatabaseConfiguration config = new DatabaseConfiguration(context);
        config.setDirectory(String.format("%s/%s", context.getFilesDir(),username));
        currentUser = username;

        if (!dbFile.exists()) {
            AssetManager assetManager = context.getAssets();
            try {
                File path = new File(context.getFilesDir()+"");
                unzip(assetManager.open("travel-sample.cblite2.zip"),path);
                Database.copy(new File(context.getFilesDir(),"travel-sample.cblite2"), "travel-sample", config);

            }
            catch (IOException e) {
                e.printStackTrace();
            }
            catch (CouchbaseLiteException e) {
                e.printStackTrace();
            }

        }
        try {
            database = new Database("travel-sample", config);
            createFTSQueryIndex();
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
    }

Try it out

  • Log into the Travel Sample Mobile app as “demo” user and password as “password”

  • Tap on "Airplane`" button to make a flight reservation

  • In the “From” airport text field, enter “London”

  • Confirm that the first item in the dropdown list is "London St Pancras" (a train station!)

    android prebuilt db