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

  • SEARCH
  • Home
  • 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 6929461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:19:16+00:00 2026-05-27T11:19:16+00:00

I cannot create my database when I close it (check code below). If I

  • 0

I cannot create my database when I close it (check code below). If I do not close it everything works fine but I get several errors in logcat saying that close() was never explicitly called. If I have created the database and then add the close() those errors will go away, but I cannot reinstall my app and run it with that.

private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_score, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                + " INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, " + KEY_SCORE1
                + " INTEGER TEXT NOT NULL, " + KEY_SCORE2
                + " INTEGER TEXT NOT NULL);");
        db.close(); <---problem
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}

public Database(Context c) {
    ourContext = c;
}

public Database open() throws SQLException {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

public long createEntry(String score, String score2) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_SCORE1, score);
    cv.put(KEY_SCORE2, score2);

    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

So the question is: how do I call db.close() and have my app run on correctly?

  • 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-27T11:19:16+00:00Added an answer on May 27, 2026 at 11:19 am

    You don’t have to call db.close(); inside your onCreate() in your DbHelper class.

    Instead you have to acquire a database instance from your DbHelper class using
    DbHelper_instance.getReadableDatabase() or DbHelper_instance.getWritableDatabase() and assign that to SQLiteDatabase object (let’s define it as SQLiteDatabase database).

    After you finish using the database, and you close your cursor, you can do something like:

            if (database.isOpen()) {
                Log.d(TAG + " Closing SQLite Database connection...");
                database.close();
            }
    

    You can put that in finally block, to be sure it will always execute (taking in mind that your DB operations are surrounded in try/catch).

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

Sidebar

Related Questions

I'm trying to create a piece of code but cannot get it working. The
I am creating a database in SQL Server 2005 and cannot remember the CREATE
I have stfw but I cannot find a simple / standalone way to create
Drupal cannot create the files folder. I tried to upload a php script with
I just want to understand why I cannot create a protected enum on C#?
This post is incorrectly tagged 'send' since I cannot create new tags. I have
One of my columns type is DateTime (Date Registered). I cannot create a query
I want to create a token generator that generates tokens that cannot be guessed
I want to create an allocator which provides memory with the following attributes: cannot
When you create a procedure (or a function) in Oracle PL/SQL, you cannot specify

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.