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 8448201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:21:49+00:00 2026-06-10T10:21:49+00:00

1st Q: I have a database that I want to read from and I

  • 0

1st Q: I have a database that I want to read from and I am pretty sure it is loading correctly since I don’t get an error message except when I tried to load it the very first time but every other time it loaded with out errors, why is that? Would this effect the app in any way on a device?

2nd Q: Is when I load the app and then click the back button to leave the app I get this error,

08-28 22:58:16.504: E/SQLiteDatabase(361): close() was never explicitly called on database '/data/data/com.example.databaseexample/databases/library_dev.db'

Am I not closing properly somewhere in my code? Thank you in advance for any help.

code:

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DataBaseHelper myDbHelper = new DataBaseHelper(this);

    try{
        myDbHelper.createDataBase();
    }catch(IOException ioe){
        throw new Error("You f'd up, the database wasn't created *crys*");
    }

    try{
        myDbHelper.openDatabase();
    }catch(SQLException sqle){
        throw sqle;
    }
}

public void createListFromDB() {
    // TODO Auto-generated method stub


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

Here is my other class I use:

public class DataBaseHelper extends SQLiteOpenHelper{

private static String DB_PATH = "/data/data/com.example.databaseexample/databases/";    
private static String DB_NAME = "library_dev.db"; 
private SQLiteDatabase myDataBase;  
private final Context myContext;


public DataBaseHelper(Context c){
    super(c, DB_NAME, null, 1);
    this.myContext = c;
}

public void createDataBase() throws IOException{

    boolean dbExist =  checkDataBase();

    if(dbExist){
        //do nothing since the database already exist.
    }
    else{
        this.getReadableDatabase();

        try{
            copyDataBase();
        }catch(IOException e){
            throw new Error("Error Copying Database");
        }
    }       
}

private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB =  SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){
        //database doesn't exist yet.
    }

    if(checkDB != null){
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

private void copyDataBase()throws IOException{

    //Opens your local db as the input stream.
    InputStream myInput =  myContext.getAssets().open(DB_NAME);

    //Path to the just created empty DB.
    String outFileName =  DB_PATH + DB_NAME;

    //Opens the empty DB as the output stream.
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfiles.
    byte[] buffer = new byte[1024];
    int length;
    while((length = myInput.read(buffer)) > 0){
        myOutput.write(buffer, 0, length);
    }

    //Close the Streams.
    myOutput.flush();
    myOutput.close();
    myInput.close();        
}

public void openDatabase() throws SQLException{

    //Open the database.
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

@Override
public synchronized void close(){
    if(myDataBase != null){
        myDataBase.close();
    }
}

@Override
public void onCreate(SQLiteDatabase db){

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){

}

}
  • 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-06-10T10:21:50+00:00Added an answer on June 10, 2026 at 10:21 am

    Question 1

    You’re having this error because the first time you’re code is trying to open a database that doesn’t exist yet..in this case it creates a new one (copying it from input file).. so yes you’ll have this error on every phone since the first time you’ll launch the application there ‘ll be no database until you create it.. btw this is a very bad approach to handle you database.

    Question 2

    This happens because you’re not explicitly closing you’re database. you should do it on your onDestroy() method.. you should add this to your DataBaseHelper

    public void closeDatabase() throws SQLException {
        // Close the database.
        myDataBase.close();
    }
    

    this method should be called within onDestroy() of your MainActivity.
    I hope this helps you

    UPDATE
    you may check this tutorial it explains very well how to handle SQLite

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

Sidebar

Related Questions

I have an application that is good from September 1st of the current year,
I have two databases. I want to dump data from one table in 1st
I have two database tables, 1st is the TABLE_GLOBAL_PRODUCTS where all products information are
I have two Linux Machine,1st is machine X ,other is machine Y i want
I have a table that is is sorted 1st by Reminder Date then ID
I have a bit of SQL that is almost doing what I want it
I have two tables in my database, 1st one contains a couple of sentences,
Ok i have a couple of tables in a database that I CAN NOT
I have a single Rails 2.2.2 app that I want to 'share' with multiple
I'm working with two databases. From 1st database in the server I'm taking all

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.