The adb and sqlite3 commands provide lightweight and convenient access to Android databases. However, they lack the features of some more robust database access tools such as a graphical interface and code completion, both of which can make development easier. When a more feature-filled database tool is desired, the SQLite database must be transferred to a development machine where it can be accessed by the database application. The adb command supports pulling files from and pushing files to and from an Android device from a connected development machine.
To copy a file from a mobile device to a development machine, the adb pull
command can be used. Listing 4.24 shows copying the databases containing the contact information to the local directory.
Listing 4.24 Pulling Contact Information with adb pull
bash-4.3$ adb pull \
5 files pulled. 0 files skipped.
1745 KB/s (713248 bytes in 0.399s) bash-4.3$
When the database folder has been copied to the development machine, any database tool that supports SQLite can be used to access the database by reading the main SQLite database file.
While using adb to pull the database from a device does allow powerful tools to be used to access the database, it means that the database needs to be copied whenever the mobile device makes changes to the database. This added step can be a little cumbersome.
Luckily, there is at least one tool, Stetho, that provides more functionality than sqlite3
while reading the database directly from the device.
Accessing a Database with Stetho
Stetho (https://facebook.github.io/stetho/) is a tool written and maintained by Facebook for Android debugging. Stetho uses the Chrome browser’s developer tools to provide access to information that can be useful when debugging an Android app. While there is a lot of data that Stetho can provide access to, the focus for this chapter is on database access.
ptg18221911 In order to use Stetho, it must be added to the target Android project and initialized in the
app. In addition, Stetho functionality is typically enabled only for debug builds as there are probably very few compelling reasons to have it enabled for release builds. This means that Stetho can be used to debug/inspect apps that are being developed but will likely not help in accessing any data, including SQLite databases, for apps that are downloaded via Google Play.
Because of this limitation, this chapter looks at using Stetho with an app that is used to track devices.
Before Stetho can be initialized, it must be added to the project build.gradle file which causes the library to be downloaded. Listing 4.25 shows the build.gradle snippet that includes Stetho as a dependency.
Listing 4.25 Adding Stetho to build.gradle
dependencies {
// other dependencies
compile 'com.facebook.stetho:stetho:1.3.1'}
}
With the dependency added to the build.gradle file, Stetho now needs to be initialized. The recommended place to initialize Stetho is on the Application class’s
onCreate() method. Because Stetho functionality should be enabled only for debug builds, it is usually a good idea to inspect the BuildConfig.DEBUG flag to conditionally enable it. Listing 4.26 shows the Application class that is used in the device database app.
Listing 4.26 Device Database Application Class
public class DeviceDatabaseApplication extends Application { @Override
public void onCreate() { super.onCreate();
if (BuildConfig.DEBUG) {
Stetho.initializeWithDefaults(this);
} } }
With Stetho initialized, the Chrome browser can be used to inspect a running application.
To inspect the app details provided by Stetho, start the Chrome Web browser and enter
“chrome://inspect” into the address bar. Figure 4.2 displays what should appear in Chrome.
ptg18221911 Exploring Databases in Android 75
Figure 4.2 Stetho device list screen
Figure 4.2 shows all of the devices that are currently attached to the development machine as well as the package name of the app that is configured to use Stetho. In this case, Figure 4.2 indicates that Chrome can attach to either a Nexus 6 or a Nexus 9 device, both of which are running the device database app. To start viewing the data provided by Stetho, click the “inspect” link under the desired device to open the Developer Tools window for the device. Figure 4.3 shows the Developer Tools window for the Nexus 6.
To access the database information on the device, click the “Resources” tab at the top of the screen, then expand the Web SQL tree in the left panel. Under the Web SQL tree, there is an entry for the database file(s) for the app. For the device database, the file is named devices.db as can be seen from the entry under Web SQL. Expanding the device tree shows the tables in the database that can be inspected by simply clicking on them. In Figure 4.3, all the rows in the manufacturer table are being displayed. This view can be useful for inspecting the contents of a table in a graphical tool without going through the trouble of pulling the database from the device.
In addition to viewing table contents, Stetho allows a developer to send SQL
statements to the database. To open up the SQL editor, click the database file name instead of a table. Figure 4.4 shows the SQL Editor in Stetho.
The Stetho SQL Editor provides a prompt that allows SQL statements to be typed in and run inline. Once a statement is run, the result of the statement is shown below it. This allows for complex queries such as JOINs to be built while connected to the database.
Accessing a database in Android can be invaluable when developing an app. Whether wrestling with the SQL syntax that will be used by Java code, or debugging a problem in the app, using tools like adb and Stetho can be vital.
ptg18221911 Figure 4.3 Developer Tools window
Figure 4.4 Stetho SQL Editor
ptg18221911
Summary 77
Summary
The use of SQLite is becoming more frequent with complex apps. To support the need to have a database to store internal data for an app, Android provides tools to ease the complexity of working with SQLite. Tools like adb along with adb shell and adb shell dumpsys can make interacting with a database on a device possible. In addition, adb can be used to transfer a database from a device to a development machine so that more powerful tools may be used to interact with the database.
To further abstract the details of communicating with an SQLite database, Android also provides APIs that support different lifecycle events of a database. The SQLiteOpenHelper
class provides convenience methods for creating, upgrading, and downgrading a database as well as configuring the connection to the database. The SQLiteDatabase class provides methods for executing raw SQL statements in SQLite.
This chapter provided some of the higher-level details for working with SQLite in Android. The rest of the book gets more into the specifics of how to implement different parts of the Android SQLite API.
ptg18221911
ptg18221911