In the part I of this post, I gave you a brief explanation of what ADB is and what is it used for. Although, ADB is a very effective tool and is used in numerous fields of Android Development, we are going to focus on only one which is, using ADB to query our Databases.
It is most likely that you have the previous application on your disk where we had created a database. If not, follow this post, and create one.
Now, to start off follow the steps:
- Launch Command Prompt and navigate to the platform-tools directory inside your SDK directory. This is where adb.exe is located
- Launch the ADB shell by typing:
- Each line should now start with a “#”.
- If you can recall, I have told you in my earlier posts that all databases of an application is located in
data -> data -> <your application name> -> databases -> <databasename>.db
- Navigate to the above location using the following command. Remember that we are not going inside the .db file now:
cd data/data/<your application name>/databases
- Now, in order to query the database, we need to execute SQLite queries. To do this, we need to start off the SQLite3 module of the ADB. Execute the following command:
- If your command is syntactically correct you should see something like this
SQLite version 3.5.0
Enter “.help” for instructions
- Each line should now start with “sqlite>”.
- List tables : In order to list all the tables present in the database, use the following command
- Now, you can execute raw SQLite queries just like you would in an SQLite RDBMS, in order to verify what data the tables in your database hold.
- Try the below query by substituting NeroTable with the name of your table.
select * from NeroTable
- The above query should present you with all the rows and columns present in the table in a formatted fashion.
This post assumes that you have a fair knowledge of SQLite queries and hence I suggest you try some of those out so that you can have a hands on practice of working with command line tools. It is quite apparent that this gives us a direct access to our databases and we do not have to resort to logs in order to view the data inside the tables as we were doing earlier. Also it prevents writing extra lines of code in our activity.
While developing complex applications containing multiple tables, ADB serves our purposes beautifully and is hence preferred.