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

Managing the database with a DataSource class

From: Android SDK: Local Data Storage

Video: Managing the database with a DataSource class

Once you've created your Database Open Helper class, the next step in following the best practices for managing SQLite is to create another class called a data source. Its methods, the data sources methods, will be called by the rest of the app. You can create multiple data sources in an application. For example, you might create one data source for each table in a database or one data source for each set of tasks. How you architect that is up to you. In my example, I only have a single table right now, so I'll use a single data source class.

Managing the database with a DataSource class

Once you've created your Database Open Helper class, the next step in following the best practices for managing SQLite is to create another class called a data source. Its methods, the data sources methods, will be called by the rest of the app. You can create multiple data sources in an application. For example, you might create one data source for each table in a database or one data source for each set of tasks. How you architect that is up to you. In my example, I only have a single table right now, so I'll use a single data source class.

I'm working in a version of the project named data source that already has a Database Open Helper class named ToursDBOpenHelper, and right now its methods are being called directly by the main activity. I'll create a new data source class, and I'll put it in the same package as the Open Helper. I'll right-click on the .db package and I'll create the new Java class and I'll name this ToursDataSource. Data source classes don't extend any particular Superclass.

In fact, my data source class won't have an explicit Superclass at all. I'll click Finish and that creates the class structure. The first step in creating the data source class is to create a public constructor method. I'll move the cursor inside the class declaration, and I'll type in public, then the name of the class, and the constructor method will receive a single argument, the context of the current activity. I'll type the name of the context class and press Ctrl+Space and import it, and I'll name the argument context.

So, that's the only constructor method that the data source class will have. In order to instantiate this class, the activity must pass in the context. Next, I'll declare the instances of the Database Open Helper and the Database. Right now, I've done this in the MainActivity class, so I'll go steal that code from the activity and bring it into the data source. I'll go to the MainActivity class. I'll expand this to full screen to make it a little bit easier to read, and here are two declarations I need, one for the Open Helper and one for the Database.

I'll copy those to the clipboard. I'll come back to the data source class and I'll paste them inside the class declaration but outside of any methods, and Eclipse automatically adds the required imports. I'll instantiate the Database Open Helper class when the data source is instantiated. So I'll add code to the ToursDataSource constructor method. Once again, I have a code that will do this. I'll go back to the MainActivity and scroll down toward the bottom of the onCreate method.

I'll take these two lines of code that are instantiating the Open Helper and the Database. I'll copy those to the clipboard. I'll come back to the data source and then I'll place the cursor inside the constructor method and paste that code into place. I need to make one change. When I instantiated the Open Helper from the activity, I passed in this as the context. This refers to the data source, not the activity, but I'm receiving the activity context right here.

So I'll just take that context argument and pass it into the Open Helper's constructor. In a way, that's all I really need to do, but I'm going to make this code even easier to use by adding another couple of methods called open and close. They'll be public methods, and the idea will be that any activity in my application can open or close the database connection anytime it wants to. So I'll create two new methods. First, I'll create a method called open. It will return void, and then I'll take that bit of code and duplicate it.

For the second version, I'll name it close. Whenever each of these methods are called, I'm going to want to log them in the LogCat console. So I'm going to set up a constant that I can use as my LogCat tag. Once again, I'll get that from the MainActivity class. I'll go to the MainActivity to the top of the class, and I'll copy the Log tag constant. Then I'll come back to the DataSource and paste it into place. Then I'll go to the open method.

I'll type in the name of the Log class, and I'll add an import, then I'll call the .i method for info. I'll pass in the LOGTAG argument and then a message of Database opened and then I'll repeat that for the close method. I'll duplicate that line of code, move it into place, and change the message to Database closed. I'll add code to actually open and close the database. The code I need to open the database is already here.

It's actually in the constructor method. When you call the getWritableDatabase method that opens the database connection. So I'm just going to take this line of code and move it into the open method. I'll hold down my Option key on Mac or the Alt key on Windows and then press the down arrow and move the code into place. Then for the close method, I'll call the Open Helper's close method. So now I have all the pieces I need for a basic data source class.

I have a constructor method that instantiates my Open Helper, and I have code to open and close the database connection. Now I'll modify my MainActivity. The MainActivity is no longer going to deal directly with the database open helper or the database. That will all be hidden inside the data source object. So I'll delete these two declarations, and I'll replace them with the declaration of an instance of ToursDataSource, my new data source class.

I'll name it datasource and I won't instantiate it yet. I'll wait 'til I get to the onCreate method. Now I'll go down to the onCreate method, and once again, I'll get rid of the code that's dealing directly with the database Open Helper and the Database, and I'll replace it with instantiating the data source. I'll pass in this as the context. Finally, if you want to maintain a persistent database connection for the entire lifetime of the activity, you can add code to the methods onPause and onResume.

These are methods that are automatically called by Android during the life cycle of the activity. As the activity comes to the screen, it's onResume method is called. So I'll go down to the bottom of the activity code, I'll place the cursor after any of the existing methods, I'll type in onResume, I'll press Ctrl+Space, and choose the onResume method. I'll get rid of the comment and then after the call to the Superclass' OnResume method, I'll call datasource.open.

Similarly, you should explicitly close the data source connection whenever the activity pauses. The onPause method is called as the activity closes down. So I'll type in onPause. I'll press Control+Space. I'll choose the onPause method that creates the override. After the call to the Superclass method, I'll call datasource.close. Now we're ready to test the whole thing. Remember, my data source is outputting to the LogCat console whenever its open and close methods are called.

So I'll easily be able to track when these operations are happening. I'll go to my LogCat console, and you can clear any existing output by clicking on the Clear Log button, then I'll run the application in the emulator. As the application comes to the screen, the onPause method is called automatically as a part of the activity life cycle. And I see the message database open. I'll leave the activity by clicking the Back button.

I'm going to move the emulator out of the way so we can see these messages. And I see the message database closed. I'll go to my application list, and I'll reopen the application, and once again, the activity opens and the database opens. I'll click the Back button and the database closes. Every activity in your application can open and close the database connection as needed. It's also useful to know that you can add as many calls to the open method as you want to.

The connection object within any activity is cached. So you don't have to worry about calling the open method too many times, but you should make sure that you're explicitly closing the connection whenever the activity is going away. That will eliminate the possibility of what are known as database connection leaks. A database connection leak can cause memory and performance issues. So by adding the code to explicitly open and close the database when needed, will make your application work well, work fast, and give you a reliable way to work with structured data.

Show transcript

This video is part of

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

29 video lessons · 6636 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.