Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6637983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:22:50+00:00 2026-05-25T23:22:50+00:00

To implement database access in my application I followed Lars Vogel tutorial , but

  • 0

To implement database access in my application I followed Lars Vogel tutorial, but I’m very confused about a couple of things…

The TodoDbAdapter class has the following constructor and open method:

public TodoDbAdapter(Context context) {
    this.context = context;
}

public TodoDbAdapter open() throws SQLException {
    dbHelper = new TodoDatabaseHelper(context);
    database = dbHelper.getWritableDatabase();
    return this;
}

And then this adapter should be initialized like this:

dbAdapter = new TodoDbAdapter(this);
dbAdapter.open();

1) The getWriteableDatabase method is the one responsible by throwing a possible SQLException. Why do we need to rethrow in our open method? Is there a reason for this?

2) What’s the point of the whole constructor/open pair? Why not initialize dbHelper and get a database ready for writing in the constructor?

3) Why do we return the instance of the object in the open method with return this? If the open method code were to be moved to the constructor, we no would no longer need to return this, it would be implicit, right? What am I missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:22:51+00:00Added an answer on May 25, 2026 at 11:22 pm

    1) The getWriteableDatabase method is the one responsible by
    throwing a possible SQLException. Why do we need to rethrow in our
    open method? Is there a reason for this?

    It’s a matter of style to explicitly declare runtime exception like this to highlight that open() might fail. If you don’t want to handle it, remove the throws clause.

    2) What’s the point of the whole constructor/open pair? Why not
    initialize dbHelper and get a database ready for writing in the
    constructor?

    This allows you to create the instance (fast operation) without being forced to do the probably slow operation (disk IO, etc.) of opening the database; most of the time this won’t matter because you’ll do both in one go as in your code snippet. Also, this keeps the constructor exception-free which some people prefer.

    3) Why do we return the instance of the object in the open method
    with return this? If the open method code were to be moved to the
    constructor, we no would no longer need to return this, it would be
    implicit, right? What am I missing here?

    If it were moved into the constructor, then yes the return this would be implicit. As the usual way to use a DB helper class in Android is to create and open it in one go, open() just does some little builder pattern so you can go TodoDbAdapter helper = new TodoDbAdapter(this).open(); for the most common use case.

    To sum up: These three points of yours are mainly about style, there’s little functional reason I can think of and definitely other ways to do it that are correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To implement database access in my application I followed Lars Vogel tutorial , but
I'm writing a database application that connects to SQL server. I'd like to implement
I have a C application using the MySQL library for database access. Is there
A new client of mine has a small VB/Access database application written in 2002
I have an application developed in C# for desktop which will access MySql Database
What I need to accomplish is a single Application with a single database but
I have a requirement to implement contact database. This contact database is special in
Task: implement paging of database records suitable for different RDBMS. Method should work for
How Do You Implement Specification Pattern for querying database using NHibernate?(without LINQ to NHibernate).I
I need ideas to implement a (really) high performance in-memory Database/Storage Mechanism in Java.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.