Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
Take your Android programming skills to the next level with the Android built-in framework that enables local data management in text files and SQLite-based relational databases. This course shows you how to create datacentric apps for Android devices, using SQLite, Java, and the built-in android.database packages. Author David Gassner describes how to define shared preferences, work with JSON and XML files in internal and external data stores, and create new local SQLite databases.
The Android Emulator is packaged with a valuable application that you can use to explore a database that's on the Emulator. It's called SQLite3, and I'll show you how to use it here. I'm running the application, and the Emulator is live. That's the first thing you need to do before you try to explore the database. Then go to a Command window. I'm working on a Mac, so I'll use Terminal. If you're working on Windows, you can run the CMD Command.
The first step is to go to the folder that contains your Android SDK. I installed the ADT Bundle and the SDK on my desktop. So, starting from my Home folder, I'll change first to the Desktop folder, and from there to the ADT Bundle folder Mine is called adt-bundle-mac, butt if you're working on Windows, it will have a different name. And then from there, I'll switch to sdk subfolder. I'll list the contents of the SDK. You want to change to the platform Tools folder.
I'll type cd, then platform- and I'll press Tab, and Terminal auto-completes the name of the folder. I'll list the contents of this folder. This folder contains Application called adb. I'll use the adb command to open a shell that lets me deal directly with the emulator's persistent storage. First, I need to know the device ID of my Emulator. To find that out, I'll run the adb command. If you're working on Mac, start with ./ and then A-D-B.
And if you're working on Windows, just type adb then after a space, type in devices. It has an ID of emulator-5554. Your emulator might have a different device. Whatever it is, make a note of it. You'll need it for the next step. Now I'll run the adb command again. Once again, I'll type ./adb and then I'll pass in the following arguments, -s, then the ID of the emulator.
Mine is emulator-5554 and then the word shell in lowercase. Press Enter or Return and now you're on the Emulator. The next step is to start up the SQLite3 command. When you execute SQLite3, you'll need to know the exact location and name of your database file. The location will always start with /data/data, but from there it will differ depending on your application package and your database file name.
For my database, I'll start with sqlite3, then /data/data. After that, type in the name of your package for your application. I'll type com.exploreca.tourfinder, then another slash, then the subfolder databases. That part is always the same. And finally, the name of your database file. In my code, I named my database file tours.db. As you type, the command will get very long and as you saw here, it will shift over to the left. That's okay. It's just what the shell does.
The command is still all there in memory, and when I press Enter or Return, I've opened SQLite3 and I'm now connected to my database. In the SQLite command environment, you can either execute commands which always start with a dot or a period, or you can execute SQL statements. I'll start with some commands. First, as shown on the screen, I'll type .help. That gives me a listing of all of the available commands for the Command Line environment.
There are commands to import and export. There are commands for inputting and outputting, for reading, and for listing tables and other schema information. For example, I'm connected to my database, and let's say I wanted to see the names of my tables. I would type .tables, and I see a listing of all the tables in the database, including my own table, tours, and another table called android_metadata which is maintained by Android.
Next, I'll get schema information. When you type the .schema command, if you just type the command itself you'll get a listing of the structure of every table in your database, but if you want to get just the structure of one table, type .schema and then the name of the table. I'll type in tours and I get back the SQL command that created the table. You can also output not just the table structure but also SQL commands that represent all of the data that's currently stored in a table.
To do this, type .dump and then the name of the table, and you'll get a listing of all of the data stated as Insert commands. You could then copy all of this content to the clipboard, save it in a Text file and you have a back-up of the entire table, both structure and data. In addition to these commands, I encourage you to explore other commands that you can find from the .help screen. You can also execute arbitrary SQL statements. For example, I'll type select title from tours where price is <= 300.
At the end of an SQL statement, type in a semicolon to indicate that the SQL statement is complete. You'll get back a listing of the retrieved data. I'll do another select statement. Select title, price from tours where price is <= 1000 order by price in descending order. Once again, I'll finish the command with a semicolon and I get back a listing of just those values. The two columns I requested and only the tours that are $1,000 or less and in descending order by price.
Again, explore what's possible. Try executing various kinds of SQL statements to see what you can get from the database from the Command Line. And finally, when you're done, here is how you get out of SQLite. Type .exit, press Enter or Return, then type exit again, this time without the dot prefix, and now you're back to your host operating system. So, that's a guided tour through the SQLite Command Line tool. You can explore your data structure and your data, find out information about your tables, and even modify the data in the database that's hosted on your Emulator.
There are currently no FAQs about Android SDK: Local Data Storage.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.