Seasonal Savings: 20% off selected memberships for a limited time. Give now

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Accessing a database from the command line

From: Android SDK: Local Data Storage

Video: Accessing a database from the command line

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.

Accessing a database from the command line

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.

Show transcript

This video is part of

Image for Android SDK: Local Data Storage
Android SDK: Local Data Storage

29 video lessons · 6610 viewers

David Gassner
Author

 
Expand all | Collapse all
  1. 6m 34s
    1. Welcome
      50s
    2. What you should know before starting this course
      2m 11s
    3. Using the exercise files
      3m 33s
  2. 21m 6s
    1. Exploring local data storage options
      5m 58s
    2. Configuring Eclipse and the Android Developer Tools
      5m 29s
    3. Creating an Android virtual device
      3m 23s
    4. Creating a new Android project
      6m 16s
  3. 31m 30s
    1. Using preferences in Android apps
      5m 28s
    2. Defining preferences with Java
      9m 31s
    3. Defining shared preferences with an activity
      10m 50s
    4. Listening for changes to shared preferences
      5m 41s
  4. 51m 13s
    1. Creating and reading files in internal storage
      10m 18s
    2. Creating and reading JSON data files
      9m 48s
    3. Working with files in external storage
      11m 22s
    4. Parsing a read-only XML file with XmlPullParser
      11m 46s
    5. Parsing a read-only XML file with JDOM
      7m 59s
  5. 1h 8m
    1. Creating a new SQLite database
      2m 52s
    2. Defining a database with SQLiteOpenHelper
      12m 12s
    3. Managing the database with a DataSource class
      9m 38s
    4. Inserting data into a database table
      10m 29s
    5. Retrieving and displaying data
      11m 44s
    6. Importing data from XML to SQLite
      5m 15s
    7. Filtering and sorting data
      9m 27s
    8. Accessing a database from the command line
      6m 46s
  6. 42m 0s
    1. Improving the data display
      9m 29s
    2. Passing user-selected data to a detail activity
      11m 36s
    3. Working with multiple database tables
      10m 28s
    4. Deleting data from database tables
      10m 27s
  7. 49s
    1. Where to go from here
      49s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Android SDK: Local Data Storage.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.