3

I am new to couchbase lite implementation, i am working on Android platform and i have created a database,saved a simple document in it. Now i want to extract the database and view the document saved in it. I placed a sample json in raw folder, read from raw and converted to hashmap. Below are all the details

CODE

DatabaseManager class to create database

package com.example.couchbasesample;


import android.content.Context;

import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Database;
import com.couchbase.lite.DatabaseConfiguration;

import java.io.File;

public class DatabaseManager {

    private final static String DATABASE_NAME = "couchbase_test";

    public Database database;

    private static DatabaseManager instance = null;

    public DatabaseManager(Context context) {

        // Set database configuration
        try {

            // Set Database configuration
            DatabaseConfiguration config = new DatabaseConfiguration(context);
            File dir = context.getDir("COUCH_ANDROID",Context.MODE_PRIVATE);
            config.setDirectory(dir.toString());

            // Create / Open a database with specified name and configuration
            database = new Database(DATABASE_NAME, config);



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

    }



    public static DatabaseManager getSharedInstance(Context context) {
        if (instance == null) {
            instance = new DatabaseManager(context);
        }
        return instance;
    }


}

MainActivity

package com.example.couchbasesample;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.couchbase.lite.MutableDocument;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.InputStream;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    private DatabaseManager dbMgr;

    @BindView(R.id.btnSave)
    Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);


        // Initialize couchbase lite database manager
        dbMgr = new DatabaseManager(this);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONObject customerJSON = new JSONObject(getFileFromResources(MainActivity.this, "samplecustomer"));
                 /*   HashMap<String, JSONObject> customerTest = new HashMap<>();
                    customerTest.put("1", customerJSON);*/
                       HashMap<String, Object> yourHashMap = new Gson().fromJson(customerJSON.toString(), HashMap.class);

                    MutableDocument doc = new MutableDocument(yourHashMap);
                    // 3. Save document to database.
                    dbMgr.database.save(doc);
                    Toast.makeText(MainActivity.this, "saved", Toast.LENGTH_LONG).show();

                } catch (Exception e) {
                    Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });
    }


    public static String getFileFromResources(Context context, String fileName) {
        String json = null;
        try {
            InputStream is = context.getResources().openRawResource(
                    context.getResources().getIdentifier(fileName,
                            "raw", context.getPackageName()));
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return json;
    }
}

WHAT I TRIED

I extracted the database using "Device file explorer" from android studio, when i save the app_COUCH_ANDROID folder and open .sqlite file in DB Browser for SQLite couldn't view the saved document instead it just shows up with random data i am unable to understand.

enter image description here enter image description here enter image description here

*Other than this i researched and found the following viewer,but unable to use this as its for MAC OS.

https://github.com/couchbaselabs/CouchbaseLiteViewer*

How i can view the couchbase lite database. Can somebody please help me out with this? Any help will be appreciated.

Juanjo Rodriguez
  • 2,103
  • 8
  • 19
User
  • 692
  • 2
  • 11
  • 29

1 Answers1

1

If you need to lookup documents stored in the database, use the getDocument or any of the Query APIs from within your app.

If you want to query the database outside of the content of the app, then you can use the cblite tool. This is a command line tool that will allow you to open and introspect the cblite2 database.

Any reason why you are trying to look up documents outside the context of the app that is embedding the database? You cannot view the data with a standard sqlite browser because the data is binary encoded and stored in Fleece format.

rajagp
  • 1,443
  • 8
  • 10