title: Unable to open sqlite database file
body: <p>Can someone help me with the reason why errors keeps on appearing. </p>
  
  <p>I have spinners and EditTexts in my activity. In that activity, it already shows the <strong>android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file</strong>. </p>
  
  <p>Here is what my <strong>Logcat</strong> says:</p>
  
  <pre><code>07-03 16:43:03.156: E/SqliteDatabaseCpp(13449): sqlite3_open_v2("/data/data/ph.com.unilab.ireport/databases/lamp_db.sqlite", &amp;handle, 1, NULL) failed
  07-03 16:43:03.164: E/SQLiteDatabase(13449): Failed to open the database. closing it.
  07-03 16:43:03.164: E/SQLiteDatabase(13449): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1123)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1074)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1050)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at ph.com.unilab.ireport.DatabaseHandler.checkDataBase(DatabaseHandler.java:81)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at ph.com.unilab.ireport.DatabaseHandler.createDB(DatabaseHandler.java:49)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at ph.com.unilab.ireport.S_9th_ISubmit.onCreate(S_9th_ISubmit.java:56)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.Activity.performCreate(Activity.java:4465)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.os.Handler.dispatchMessage(Handler.java:99)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.os.Looper.loop(Looper.java:137)
  07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.ActivityThread.main(ActivityThread.java:4514)
  </code></pre>
  
  <p>I can populate the data from the database to spinner, but however, wherever I press the Save button, the values from the widgets didn't saved to the database.</p>
  
  <p><strong>databaseHandler.java</strong></p>
  
  <pre><code>public class DatabaseHandler extends SQLiteOpenHelper {
  static final String TAG = "LAMP";
  public SQLiteDatabase db;   
  private final Context myContext;    
  private static DatabaseHandler dbHandler;
  
  
  public DatabaseHandler(Context context) {
      super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
      this.myContext = context;
  }
  
  public static DatabaseHandler getInstance(Context context) {
          if(dbHandler == null) {
          dbHandler = new DatabaseHandler(context);
          }
      return dbHandler;
      }
  
  
      public void createDB() throws IOException{
      boolean dbExist = checkDataBase();
      if(dbExist) {
              //do nothing - 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 =Constants.DB_PATH + Constants.DB_NAME;
          checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
       }catch(SQLiteException e){  
              //database does't exist yet.     
      }
  
          if(checkDB != null){
  
              checkDB.close();
  
          }
  
          return checkDB != null ? true : false;
      }
  
  
      private void copyDataBase() throws IOException{
  
          //Open your local db as the input stream
          InputStream myInput = myContext.getAssets().open(Constants.DB_NAME);
  
          // Path to the just created empty db
          String outFileName = Constants.DB_PATH + Constants.DB_NAME;
  
          //Open the empty db as the output stream
          OutputStream myOutput = new FileOutputStream(outFileName);
  
          //transfer bytes from the inputfile to the outputfile
          byte[] buffer = new byte[1024];
          int length;
          while ((length = myInput.read(buffer))&gt;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 = Constants.DB_PATH + Constants.DB_NAME;
          db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  
      }
  
      @Override
      public synchronized void close() {
  
              if(db != null)
                  db.close();
  
              super.close();
  
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
  
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  
      }
  </code></pre>
  
  <p>Can you help resolve this error?</p>