Debug Database in Android

In my last tutorial, we have learned how we can use persistence database in android. We also understood which database wrapper we can use in our application. Its all depend on our application that how frequently communicating with the database. If my application is a big usage of the database for online and offline sync then I would recommend using Green Dao ORM otherwise we can use Android Active ORM for a small application. Here are the link detail for GreenDao Fast ORM in Android and Active Android ORM in android.

Now, this tutorial is based on an android database. Many of android developer asked about how to debug our database if the application running on my emulator. To debug database there are many ways you can, But some of the very strong pattern we can debug the database of my application.

1. Sqlite3 by using ADB: 

There is a command line version of sqlite3 available that allow you to directly access, view and modify SQLite Database in the ADB root shell on the Android device without having to copy your database on a computer or anywhere.

To view or modify the database by Sqlite3 you need to follow few steps. Here are steps
1. Open ADB shell (You can find the ADB from the Android SDK installed location, for example,         user/AppData\Local\Android\Sdk\platform-tools). Now you need to start this ADB from a                   terminal window or cmd.

2. Open command prompt and change directory to type this                                              cd C:\Users\username\AppData\Local\Android\Sdk\platform-tools
Then You will reach to this directory path like that:
C:\Users\username\AppData\Local\Android\Sdk\platform-tools>

3. Enter command: adb shell 
Once you enter above command you can find the device name. You can check all databases which exist on this location. You can use command .databases

4. Now you can use this command: sqlite3 databaseName.db

5. Now You can see all table exist in this database. What you need to give command .tables

Now You can add or modify data in a table and fetch data from a table. You just need to enter below command.

sqlite3 > select * from table1;
     
Even if not know about the existence command of sqlite3 then you just get a help to enter command .help
I hope You can debug your database easily by using ADB.

2. A Fork on a local Computer by ADB Command:

This also one of a great approach to see your database schema and debug your query. Ok, let's see how?
What you need to do that just fork your database on a local computer on simple one command.Just One thing make sure that what DB name with the extension you give, ex. Dbname.sqlite, dbname.db

adb -d shell 'run-as com.yourPackageName cat /data/data/com.yourPackageName/databases/dbname.sqlite > /sdcard/dbname.sqlite'

That's it It will fork your database on a local machine and open this database in any open source tool. SQL queries. available to download at DB Browser for SQLite. 

3. Android Debug Database Tools- 



Tools are the best way to debug database on GUI presentation. I will give you some of the debug database tools that help your life pretty easy and simple.

1. Android-Debug-Database is one of the best tools to debug your complex database. It provides the GUI interface to check your database schema. And its configuration is also very simple.
What you need to do just follow few simple steps to configure.

Step 1: Add the following dependency to your app's Gradle file.

     
debugCompile 'com.amitshekhar.android:debug-db:1.0.0'

Step 2: Build and run your app.


Step 3: Now look into the Android studio log cat. You can get one URL link of localhost. What you need to do just copy this URL and use your IP address to browse the database. Open in your browser the URL: http://YOUR_PHONE_IP_ADDRESS:8080 and you will be presented there.I would be recommended please check for more details in the official documentations.


2. SQLiteOnWeb-Android is also similar to Android Debug databases tool. You can get help from an official site.

4. DatabaseManager for Android:

This is one of the best kind of java library to get the database tables on android UI. A configuration is a little bit difficult for the beginner but Official Documentation explained better for your understanding.

It's a single Java activity file; just add it to your source folder. You can view the tables in your app database, update, delete, insert rows to your table. Everything from inside your app. When the development is done remove the Java file from your src folder. That's it.

For a better understanding, it provides the video also. You can view the 5-minute demo, Database Manager for Android SQLite Database. Here is the video detail.




5.A Debug Bridge for Android Application:

This is one of the best tools which is given by facebook for debugging android database. Here is Stetho Official Documentation that well designed and described. You need to add few dependency files in your Gradle file.


dependencies {
  // Stetho core
  compile 'com.facebook.stetho:stetho:1.5.0'        
  //If you want to add a network helper
  compile 'com.facebook.stetho:stetho-okhttp:1.5.0'
}

Great. Now you need to add few lines in your AminApplication file.

public class MyApplication extends Application {
  public void onCreate() {
    super.onCreate();
    Stetho.initializeWithDefaults(this);
  }
}
Great. Even you better configuration then you use Custom plugins. Custom plugins are the preferred means of extending the dump app system and can be added easily during configuration. Simply replace your configuration step as such:

public class MyApplication extends Application {
  public void onCreate() {
    super.onCreate();
    Stetho.initialize(Stetho.newInitializerBuilder(context)
    .enableDumpapp(new DumperPluginsProvider() {
      @Override
      public Iterable get() {
        return new Stetho.DefaultDumperPluginsBuilder(context)
            .provide(new MyDumperPlugin())
            .finish();
      }
    })
    .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(context))
    .build());
  }
}

For an extra bonus, if you looking something awesome then use this plugin. But it is paid.Here is the Idescout Official Documentation.

I hope that all will help you to better understanding in debugging the database.

Comments

  1. Android Vertical Stepper Form Library
    It may display a transient feedback message after a step is saved. And today we’re going to see a cool library which help you to implement that stepper form in your applications. This Android library implements a vertical stepper form following Google Material Design guidelines.
    http://www.tellmehow.co/android-vertical-stepper-form-library/

    ReplyDelete

Post a Comment

Popular posts from this blog

NavigationView Drawer Expandable menu Item in ANdroid

Expandable Item In RecyclerView in Android

Custom SeekBar (Discrete SeekBar) in android